The Monty Hall Puzzle, with Proof

The Monty Hall problem is a famous probability puzzle based on a game show scenario. Here’s how it goes:
- Setup: You are presented with three doors. Behind one door is a car (the prize), and behind the other two doors are goats.
- Initial Choice: You choose one of the three doors, say Door 1.
- Reveal: The host, Monty Hall, who knows what’s behind each door, opens one of the remaining two doors, revealing a goat. For example, if you chose Door 1, and there is a goat behind Door 2, Monty will open Door 2.
- Decision: You are then given a choice: stick with your original selection (Door 1) or switch to the other unopened door (Door 3).
The puzzle lies in determining whether you should stick with your initial choice or switch doors.
Solution: It is actually more advantageous to switch doors. Here’s why:
- When you make your first choice, there is a 1/3 chance that you picked the car and a 2/3 chance that you picked a goat.
- If you initially chose a goat (which has a 2/3 probability), Monty will always be able to open the other door with a goat. Switching in this case means you win the car.
- If you initially chose the car (with a 1/3 probability), switching means you lose, as Monty’s reveal doesn’t affect your initial correct choice.
Thus, by switching, you have a 2/3 chance of winning the car, while sticking with your original choice gives you only a 1/3 chance. This counterintuitive result often surprises many people and is why the problem has become so well-known in probability theory discussions.
Monty Hall Puzzle Proof
Here’s a PowerShell script that you can run to prove the theory that switching is the better strategy. How does it work? It runs a simulation of 100 times each of switching the door, and 100 times each of sticking with the initial door. The output shows how many wins out of 100 each strategy achieved for each run of the script.
class MontyHallSimulator {
[int]$Iterations;
MontyHallSimulator([int]$iterations) {
$this.Iterations = $iterations;
}
[int] Simulate([bool]$switchDoor) {
$wins = 0;
for ($i = 0; $i -lt $this.Iterations; $i++) {
# Randomly place the car behind one of the three doors (1, 2, or 3)
$carPosition = Get-Random -Minimum 1 -Maximum 4;
# Player makes an initial choice (randomly select one of the doors)
$initialChoice = Get-Random -Minimum 1 -Maximum 4;
# Monty reveals a door with a goat behind it
$possibleRevealChoices = @(1..3) | Where-Object { $_ -ne $carPosition -and $_ -ne $initialChoice };
$revelation = Get-Random -InputObject $possibleRevealChoices;
# Determine the player's final choice
if ($switchDoor) {
# Switch to the remaining door
$finalChoice = @(1..3) | Where-Object { $_ -ne $initialChoice -and $_ -ne $revelation };
} else {
# Stick with the initial choice
$finalChoice = $initialChoice;
}
# Check if player wins (if final choice matches car position)
if ($finalChoice -eq $carPosition) {
$wins++;
}
}
return $wins;
}
}
# Create an instance of the class with 100 iterations
$simulator = [MontyHallSimulator]::new(100);
# Simulate 100 iterations of switching doors
$switchWins = $simulator.Simulate($true);
# Simulate 100 iterations without switching doors
$stickWins = $simulator.Simulate($false);
Write-Output "Switching strategy wins: $switchWins out of $($simulator.Iterations)";
Write-Output "Sticking with initial choice wins: $stickWins out of $($simulator.Iterations)";
Example output:

One could expand this to run the simulation several thousand times and then save the data to a CSV or JSON file for charting the data and visualizing it.

It all started when I watched my dad fix a very expensive RC car after I ran it to death, when I was a kid.