The Monty Hall paradox is a paradox because it is counter-intuitive even though it is mathematically correct. This article demonstrates with PHP code how the paradox works.
You as a contestant on a game show get to pick one of three doors.
A big prize is behind just one door, while the other two contain worthless things. After making your selection, the host reveals one of the doors you did not pick always revealing junk. You now have a choice to switch your chosen door.
Intuitively, we say that each door has a 33.33% chance of being the prize, but this is only correct in the beginning.
Many believe that after revealing one of the worthless doors, that the two remaining doors have a 50% chance. This is not the case.
After the reveal of a worthless item, the remaining door you did not pick has a 66.67% chance of being the big prize. The door you picked still has a 33.33% chance. This is because of two facts:
- The two doors you did not pick together had a 66.67% chance of being the big prize.
- And they still do, you just know what is behind one of them.
This is some simple PHP code that runs the Monty Hall experiment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $Runs = 10000000; $PrintDivisor = 1000000; $Options = ["Door 1", "Door 2", "Door 3",]; $Correct = 0; for ($iteratorRun = 1; $iteratorRun < $Runs; $iteratorRun++) { $Chosen = $Options[array_rand($Options)]; $Winner = $Options[array_rand($Options)]; // Reveal a worthless door. $rDiff = array_diff($Options, [$Winner, $Chosen]); $Reveal = $rDiff[array_rand($rDiff)]; // Switch to a remaining door. $sDiff= array_diff($Options, [$Reveal, $Chosen]); $Switch = $sDiff[array_rand($sDiff)]; if ($Switch === $Winner) { $Correct++; } if ($iteratorRun % $PrintDivisor === 0) { echo "After $iteratorRun rounds we won $Correct times! (" . round($Correct / $iteratorRun * 100, 5) . "%)" . PHP_EOL; } } echo "After $Runs rounds we won $Correct times! (" . round($Correct / $Runs * 100, 5) . "%)" . PHP_EOL; |
This script will output something similar to the following:
1 2 3 4 5 6 7 8 9 10 |
After 1000000 rounds we won 666866 times! (66.6866%) After 2000000 rounds we won 1334596 times! (66.7298%) After 3000000 rounds we won 2001677 times! (66.72257%) After 4000000 rounds we won 2667770 times! (66.69425%) After 5000000 rounds we won 3334176 times! (66.68352%) After 6000000 rounds we won 4001079 times! (66.68465%) After 7000000 rounds we won 4667219 times! (66.67456%) After 8000000 rounds we won 5334197 times! (66.67746%) After 9000000 rounds we won 6000247 times! (66.66941%) After 10000000 rounds we won 6666854 times! (66.66854%) |
But what if there were four doors? Well, just add another options to $Options
on line 5.
1 2 3 4 5 6 7 8 9 10 |
After 1000000 rounds we won 375613 times! (37.5613%) After 2000000 rounds we won 750925 times! (37.54625%) After 3000000 rounds we won 1125899 times! (37.52997%) After 4000000 rounds we won 1501413 times! (37.53533%) After 5000000 rounds we won 1876312 times! (37.52624%) After 6000000 rounds we won 2251002 times! (37.5167%) After 7000000 rounds we won 2625728 times! (37.5104%) After 8000000 rounds we won 3000856 times! (37.5107%) After 9000000 rounds we won 3375764 times! (37.50849%) After 10000000 rounds we won 3751245 times! (37.51245%) |
Why 37.50%? The logic is the same as it was for three doors. With four doors, the door you pick has a 25% chance, and the three other doors have a 75% chance of being the winner. When one of the three doors is eliminated the two remaining doors share a 75% chance of winning.
75% / 2 = 37.50%
This follows with five doors.
1 2 3 4 5 6 7 8 9 10 |
After 1000000 rounds we won 266404 times! (26.6404%) After 2000000 rounds we won 533343 times! (26.66715%) After 3000000 rounds we won 799895 times! (26.66317%) After 4000000 rounds we won 1066398 times! (26.65995%) After 5000000 rounds we won 1332320 times! (26.6464%) After 6000000 rounds we won 1599242 times! (26.65403%) After 7000000 rounds we won 1866010 times! (26.65729%) After 8000000 rounds we won 2132973 times! (26.66216%) After 9000000 rounds we won 2400264 times! (26.6696%) After 10000000 rounds we won 2666638 times! (26.66638%) |
80% / 3 = 26.67%
And so on.