Monty Hall Problem Statistics Algorithm
Today the goats made it to the front page of reddit yet again, this time it’s a forum post with over 9000 replies.
Because I love winning, I decided to settle this once and for all, the Mythbusters way: 10000 games, 20000 cars, 10000 goats, 30 PHP lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php $games=10000; $win=0; //number of wins if I don't switch $winswitch=0; //number of wins if I switch for ($i=1;$i<=$games;$i++) { $doors=array(0,1,2); //create the doors $doorstemp=array(0,1,2); //doors in the hosts mind $prize=rand(0,2); //the prize is behind this door $choice=rand(0,2); //the door number I choose unset($doorstemp[$choice]); //the host must not open one door that I chose unset($doorstemp[$prize]); //the host must not open the door where the prize is... $hostopensfirst=array_rand($doorstemp); //the host decides what door to open unset($doors[$hostopensfirst]); //the host opens a door with a cheap prize //do not switch if ($choice==$prize) { $win++; } //switch unset($doors[$choice]); //discard my first choice $choice=array_pop($doors); //select the alternative if ($choice==$prize) { $winswitch++; } } echo("Playing $games games. If you:\n"); echo("-hold you win : $win goats.\n"); echo("-switch you win: $winswitch goats"); ?> |
The Monty Hall problem is a probability puzzle based on the American television game show Let’s Make a Deal. The name comes from the show’s host, Monty Hall. The problem is also called the Monty Hall paradox, as it is a veridical paradox in that the result appears absurd but is demonstrably true.
*Modified for lolz
Each game should have 2 goats and 1 car (not 2 cars and 1 goat) – if the host shows you the goat, then both other doors should be hiding a car (the only two options left). Also the result should be that switching is better http://en.wikipedia.org/wiki/Monty_Hall_problem . Are you sure when you modified it for lulz, you didn’t make a mistake?