> ## Content Index
> Fetch the complete content index at: https://starikov.co/llms.txt
> Use this file to discover other available public pages before exploring further.

# The Monty Hall Problem
- URL: https://starikov.co/monty-hall-problem/
- Published: 2016-09-16T21:17:00.000Z
- Updated: 2026-06-09T23:01:29.000Z
- Author: Illya Starikov
- Tags: Code, Mathematics, #u-color-bg-tag-accent, #u-color-text-dark

# The Monty Hall Problem

You've seen it in movies or read about it in books: the Monty Hall Problem. Here's how it works: you're on a game show. There are three doors—behind one is a prize, behind the other two is nothing but disappointment. You pick a door. The host, Monty Hall, opens one of the other doors and reveals there's no prize behind it. Keeping the other two doors closed, he asks if you'd like to switch your choice or stick with your original pick. What should you do? Ben Campbell has a solution.

You mean to tell me that switching mid-game—an offer from the man who set up all the doors and prizes—gives me better odds of winning? How is this possible? Seems a bit counterintuitive. Seems almost wrong. Fortunately, we can test this with a simple program.

```swift
import Darwin // for arc4random_uniform

func random(from from: Int, to: Int,
            except: Array<Int>? = nil) -> Int {
    // Produce bounds, +1 to make inclusive
    let number = Int(arc4random_uniform(UInt32(to - from + 1))) + from

    if except != nil && except?.indexOf(number) != nil {
        // Recursively call until we find a valid number
        return random(from: from, to: to, except: except)
    }

    return number
}

// Returns if you won when you stayed and switched, respectively
func montyHall(doors: Int) -> (Bool, Bool) {
    let originalDoor = random(from: 1, to: doors)
    let correctDoor = random(from: 1, to: doors)

    // The door the host reveals
    let revealedDoor = random(from: 1, to: doors,
                              except: [correctDoor, originalDoor])
    // If you switched, this would be your new door
    let switchedDoor = random(from: 1, to: doors,
                              except: [revealedDoor, originalDoor])

    return (originalDoor == correctDoor, switchedDoor == correctDoor)
}

let testValues = [100, 1_000, 10_000, 100_000,
                  1_000_000, 10_000_000]
let actualNumberOfDoors = 3

for value in testValues {
    var stayingCounter = 0, switchingCounter = 0

    for _ in 0...value {
        let (stayedWon, switchedWon) = montyHall(actualNumberOfDoors)
        if stayedWon { stayingCounter += 1 }
        if switchedWon { switchingCounter += 1 }
    }

    let output = "Test Case: \(value). " +
                 "Staying Won: \(stayingCounter), " +
                 "Switching Won: \(switchingCounter)"
    print(output)
}

```

There's no tricks in the code. No gimmicks. So, the results?

| Input    | Staying Won | Switching Won |
| -------- | ----------- | ------------- |
| 100      | 42          | 58            |
| 1000     | 344         | 656           |
| 10000    | 3289        | 6711          |
| 100000   | 33203       | 66797         |
| 1000000  | 333178      | 666822        |
| 10000000 | 3335146     | 6664854       |

It appears that Ben\[1\] was right. Taking the limit as the input gets higher, switching won roughly \\(66\\%\\) of the time—compare that to the original \\(33\\%\\) you got before you were given the option to switch doors. So, how is this possible? I'll explain.

![](https://starikov.co/content/images/2025/11/monty_hall.png)

For simplicity, suppose the prize is behind door A out of doors A, B, C. The argument can be made for any initial door, but A makes it easier. At this point, you have a \\(\\frac{1}{3}\\) chance of picking the prize no matter what you pick. If you pick door A, the door which contains the prize, the host will definitely want you to change so he will offer you either door B or C. Now suppose you choose a door without the prize, either door B or C. The host has *no choice* but to *eliminate the door without the prize*. Meaning if you switch, you have the winning door.

So, why \\(66\\%\\)? Computationally, if you always switch and you picked the wrong door initially, your switch will win every time. There are two incorrect choices out of three, meaning \\(\\frac{2}{3}\\) of the time you will win.

You've just had your first lesson in conditional probability.

---

1. 21 main character, played by Jim Sturgess ↩︎