The Monty Hall Problem (With Free Bonus Simulator)

by chrisc

Part I: The Background

You are presented three doors: Door #1, Door #2, and Door #3. Behind one of these three doors is... one million dollars! (a.k.a. The Prize) Needless to say, you'd like to win this. Behind the other two doors are goats, or nothing, or dot com stock certificates, or whatever else you don't particularly care to own. You are given a chance to choose a door to claim a prize from; you point to your chosen door. The game show host looks you right in the eye and says: "Is that your final answer?" "No," you reply, and he turns to the doors and with a dramatic flourish, opens one of the two doors you did not choose. You see a goat. He now offers you one last chance: stay with your initial choice ...or... switch to the other closed door. Which gives an edge?? Does it even matter?

Well, since it's worth writing about, the answer is, of course, that it does matter. It is true that, no matter what your first guess, your odds of winning the prize are higher if you switch to the other door. For example, you first chose Door #1, he shows you Door #2 has nothing behind it, so, to increase your chances of winning, you should say, no, no, I was wrong, I'd rather have whatever is behind Door #3.

How on earth can this be, you say? Has he not simply left you with the same odds you would have had if there had only been two doors to start with? 50/50? No, as it turns out. In making your first choice, then subsequently having him open one of the doors you did not choose, you have gained an edge.

Part II: The Proof

Essentially, the reason for this dilemma stems from the funny way something called conditional probability works. Look at the problem. The prize-winning door is pre-selected. You then point at any door as your initial choice. What are your chances of being right? Exactly 1/3. Looked at differently, you have 2/3 odds that you are wrong. Now, what does your friendly local game-show host offer? He basically says, pick two doors, (since you point to one out of three, you might as well have asked him to open one of the other two) and I'll tell you at least one of them that is wrong. Essentially, at this point, there are two states: (1) You've guessed correctly the first time. Oooh -- ouch, but it can't be helped. In this state, switching always loses... since you're switching away from the right answer... or (2) you're wrong the first time, and are quite helpfully shown how to eliminate one of the potentially correct other answers. Now, you of course have no way of knowing which of these situations you're in until it's all over, but look at the chances: (1) You're right, and should stay, 1/3 of the time. (2) You're wrong, and thus will definitely win if you switch, 2/3 of the time... hey! Look at that! If you stay always, you win one in every three games. If you switch always, you win two in every three games... I like those odds....

Part III: The Simulator

On this page is a simulator that plays this games as many times as you'd like; you simply enter the number of times you'd like the simulation to run, and out pops a neat little report of the percentage of games you would have won had you obstinately stuck with that first door you chose and the percentage of games you would have won had you switched doors like I told you to. To get decent results, you probably need on the order of at least 10,000 tries; 1,000,000 seems to give pretty consistent numbers. This is empirical in the extreme; I haven't thought through the hard statistics of the simulator or the game for a while, so this is just a quick-and-dirty makeshift example. Note also that this is in no way a proof of any sort. This is simply an example-generator. Maybe I'll add statistics and a proof later. One more note on the simulator. It's a JavaScript. Read: slow and clunky. Too big a number will probably slow down or freeze your browser for a while. Your browser may also complain about this; tell it to keep going anyway... Have fun!

Number of trials:

Part IV: Under the Hood of the Simulator

If you're at all interested in how this simulation works, or maybe are just hell-bent on keeping me honest, here is the code of the program. It is written in a simple language called "JavaScript" that runs on most web browsers. It should be fairly intuitive to follow, and there are comments explaining what each segment of the program does. Comment lines are always preceded by a //, and here I've nicely highlighted them all in red. Here's some JavaScript basics:

  • var somewierdname; 
    declares a variable, which holds a number,or text, or other types of data. It can be set using the = sign.

  • somevariable++;
    increments 'somevariable' by one.

  • for (var index = 1; index <= 10; index++) 
      {
      dosomestuff(index);
      }
    counts from 1 to 10. 'index' holds the current number, and the stuff inside the { }'s is repeated for each number. Thus, 'dosomestuff(index);' is done first with one, then with two... and so on.

So, there you go; here's the actual code of the program that is embedded in this page:

function testem(numtries)
// 'testem' takes the number of trials desired and repeats the 
// simulation that many times, finally printing the results nicely on
// the web page.  
  {
  var staywin = 0;
  
  // Save starting time
  var start = new Date();
  
  // Repeat the simulation 'numtries' times
  for (var trial = 1; trial <= numtries; trial++)
    {

    // Generate two random integers in [0,2]; one represents the door # the prize
    // is behind, the other represents the door # you first pick.  Thus, if you 
    // first pick the correct door, (i.e. the two random numbers are equal) you 
    // win if you 'stay' (n.b. 'staywin' thus goes up by one).  Otherwise, you 
    // chose poorly, and switching will always win, since the third door is chosen 
    // for you.  Final probabilities are calculated from the 'staywin' number.  

    if (Math.floor(Math.random() * 3) == Math.floor(Math.random() * 3))
      staywin++;

    }
  
  // Save ending time
  var end = new Date();

  // Format the numbers into pretty results
  var conclusions = "Ran " + numtries + " trials ";
  conclusions += "starting at " + start.getHours() + ":" + start.getMinutes() + ":" + start.getSeconds(); 
  conclusions += " lasting " + Math.round((end.getTime() - start.getTime()) / 1000) + " seconds\n";
  conclusions += "~~~~~~~~~~~~~~~~~~~~\n";
  conclusions += "  'Staying' every time would have won " + Math.round(10000 * staywin / numtries) / 100 + "% of the games\n";
  conclusions += "'Switching' every time would have won " + Math.round(10000 * (numtries - staywin) / numtries) / 100 + "% of the games\n\n";

  // Output the conclusions
  document.forms[0].output.value = conclusions + document.forms[0].output.value;
  }
Last modified Sunday April 27, 2003