The Monty Hall Problem
"yes")// initialize { $thecar=rand(1,3); $step=1; if($numswitch=="") { $numswitch=0; $numstay=0; $numswitchwin=0; $numstaywin=0; } print "
"; print " "; print "
There is a car behind one door...goats behind the other two.
Door 1 Door 2 Door 3
"; print "Step 1: Select a door: "; print "
"; } elseif($step==2) { if($yourpick==$thecar) { $goatdoors=str_replace("$thecar","","123"); $rander=rand(0,1); $unrander=1-$rander; $wereveal=substr($goatdoors,$rander,1); $theoneleft=substr($goatdoors,$underander,1); } else // we have no choice { $wereveal=str_replace("$yourpick","",str_replace("$thecar","","123")); $theoneleft=$thecar; } print "
"; print " "; print ""; for($i=1; $i<4; $i++) { print ""; } print ""; for($i=1; $i<4; $i++) { print ""; } print "
There is a car behind one door...goats behind the other two.
Door $i"; if($i==$yourpick){ print " (your pick) "; } print "
"; if($i==$wereveal){ print ""; } else { print ""; } print "
"; print "Step 1: Select a door: you picked door $yourpick

"; print "Step 2: We reveal for you the goat behind door $wereveal!

"; print "Step 3: Now you have a choice. You can either stay with door $yourpick or switch to $theoneleft.
"; print "

"; } elseif($step==3) { if($thecar==$yourpick) { if($switchq=="switch") { $youwin="no"; } else { $youwin="yes"; } } else { if($switchq=="switch") { $youwin="yes"; } else { $youwin="no"; } } print "
"; print ""; for($i=1; $i<4; $i++) { print ""; } print ""; for($i=1; $i<4; $i++) { print ""; } print "
There is a car behind one door...goats behind the other two.
Door $i"; print "
"; if($i==$thecar){ print ""; } else { print ""; } print "
"; print "Step 1: Select a door: you picked door $yourpick

"; print "Step 2: We reveal for you the goat behind door $wereveal!

"; print "Step 3: "; $theoneleft=str_replace("$yourpick","",str_replace("$wereveal","","123")); if($switchq=="switch") { print "You decided to switch to door $theoneleft."; $numswitch++; mysql_query("update montyhall set numswitch=numswitch+1"); if($youwin=="yes"){ $numswitchwin++; mysql_query("update montyhall set numswitchwin=numswitchwin+1"); } } else { print "You decided to stay with your original choice of $yourpick."; $numstay++; mysql_query("update montyhall set numstay=numstay+1"); if($youwin=="yes"){ $numstaywin++; mysql_query("update montyhall set numstaywin=numstaywin+1"); } } print "

"; print "Step 5: "; if($youwin=="yes") { print "CONGRATULATIONS! You got the car!"; } else { print "SORRY, you got a goat!"; } print " "; $perwsw=round(100*$numswitchwin/($numswitch+.001)); $perwst=round(100*$numstaywin/($numstay+.001)); print "

These are your results so far:

Mathematics predicts that you will win twice as often by switching! To see this effect you have to play many times. (Try to stay and switch at least 10 times each to see it work well.)
Press the button to play again: .

(Or, click here to see the statistics for all of the people who have played since May 17, 2005.)"; print ""; } } elseif($pg=="stats") { $result=mysql_fetch_object(mysql_query("select * from montyhall")); $numswitch=$result->numswitch; $numswitchwin=$result->numswitchwin; $numstay=$result->numstay; $numstaywin=$result->numstaywin; $clicks=$numswitch+$numstay; $perwsw=round(100*$numswitchwin/($numswitch),4); $perwst=round(100*$numstaywin/($numstay),4); print "

This game has been played $clicks times since it was first posted on May 17, 2005. These are the results so far:

Mathematics predicts that you will win twice as often by switching! To play yourself, click here."; } elseif($pg=="explain") { print "

What is the mathematical explanation for the fact that switching is a better strategy than staying with the originally chosen door in the Let's Make a Deal scenario?

Using probability in a situation like this, where there are a finite number of possibilities with equal likelihoods, basically amounts to counting how many different ways there are to do something and in how many of those situations you get the desired outcome. The probability of winning is then the number of ways to get the desired outcome divided by the total number of possible scenarios!

Let's consider two people playing the game: Mr. Steed (who always stays with his first choice) and Mr. Switzer (who always switches after the goat is revealed). Neither of them knows that the car is really behind door number one. So, let's compute the probabilities that they will will by looking at how many possibilities there are and in how many of those they will win.

Mr. Steed has three choices; he can pick door number one, two or three. So, the number of possibilities (and therefore the denominator of the probability that he will win) is 3. To get the numerator we need to work out when he will win. If he picks door number one, he will win because the car is behind it and he is going to stay with his first choice. (He's a very confident guy!) However, if he picks two or three as his door he will go home with a goat. So, his probability of winning is 1/3. This means that if he plays with his strategy for a long time, he'll win just about 33 percent of the time.

On the other hand, Mr. Switzer has a different probability of winning! Like Mr. Steed, he has three choices: door one, two or three. And so, again, the denominator of his likelihood of winning is going to be the same number 3 as before. But, if Mr. Switzer picks door number one he will not get the car. After he picks door number one, Monty will reveal a goat behind one of the two other doors (it doesn't matter which one) and Mr. Switzer (true to his name) will switch to another door...he will switch away from the car. (Sounds bad, right?) But wait, if Mr. Switzer picks door number two, then Monty will show him the goat behind door three and Mr. Switzer will switch to door number one and get the car! So, he will win if he picks door number two. In fact, he'll also win the car if he picks door number three for the same reason (Monty will open door two and Switzer will switch to door one). So, Mr. Switzer's chances of winning are 2/3. He will win about 66 percent of the time...twice as often as Mr. Steed.

Yes, this might seem counter-intuitive. But, it makes sense because Monty is revealing information to you about the two doors that you do not pick, but gives you no information about the door you picked. Essentially, when the door with the goat is revealed, the 1/3 probability that the car was behind that door all gets transfered to the door that you can switch to since Monty only had the choice of opening one of those two and never opens the door that the contestant picked.

Still don't believe me? Try playing the game yourself. Try each of the strategies at least 10 times and see what happens! Or, look at the statistics so far for all of the times the game has been played on this webpage."; } else { print " On the old American game show Let's Make a Deal, there was always a segment in which the contestant had to pick one of three doors, looking for the car which was behind one of them and hoping not to get one of the goats that were behind the other two. The rules were as follows: The contestant picks a door, but before the door is opened, the host opens one of the doors that was not selected and reveals a goat. Now there are still two doors left, the one originally selected and another. The contestant again has the choice of which of the two doors to pick!

The interesting thing about this situation is that most people think that it does not matter whether you switch doors or keep the same door at this point. (This is why this was mentioned on a recent episode of the TV show NUMB3RS.) \"What difference does it make?\" they might say. \"After all, there's a 50% chance of the car being behind either of the two doors...right?\"

WRONG! It doesn't take too much mathematics to see that you are twice as likely to win if you switch doors at that point. I will try to convince you of this fact in two ways:

"; } //print "
//debug: thecar=$thecar yourpick=$yourpick wereveal=$wereveal //
"; print "
About Monty Hall Problem / Play the Game / Explain / Statistics / Mathematical Fiction
Page written and maintained by Alex Kasman (2005) "; ?>