A javascript simulator for The Monty Hall Problem

Problem Statement:

A thoroughly honest game-show host has placed a car behind one of three doors. There is a goat behind each of the other doors. You have no prior knowledge that allows you to distinguish among the doors. “First you point toward a door,” he says. “Then I’ll open one of the other doors to reveal a goat. After I’ve shown you the goat, you make your final choice whether to stick with your initial choice of doors, or to switch to the remaining door. You win whatever is behind the door.

In search of the car, you begin by pointing to door number 1.

The host shows you that door number 3 has a goat.

Do your odds of getting the car increase by switching your choice to door 2?

Algorithm:

The simulator uses the following algorithm:

repeat i = 1 to nIterations:

   1. Randomly pick a door to be the winning door,
   2. Randomly pick a door for the player,
   3. Randomly pick the door(s) that the host would open, assuming that:
          3.1 he will never open the door with the car behind it, and
          3.2 he will never open the door picked by the player.
          3.3 he will open all except two doors before asking the player to choose
                 again. (ie, if there are 3 doors he will open 1 door, leaving 2 doors
                 closed, then ask the player to choose again)

   4. Check and log if the player has won by switching / sticking.

end.

Simulator:

* For better results, please ensure that the number of iterations (attempts) is much greater than the number of doors.
* Please don’t enter less than 2 doors, the simulator will hang (not validated) .

Result:

Surprisingly, the simulator shows that the players odds of winning increase, infact double, if switching doors, for a 3 door simulation.

The full explanation / reason for this can be found at the Wikipedia.

For the [alt+v] C/O impaired, the Javascript Source for the simulator is available for download.

9 Comments on “A javascript simulator for The Monty Hall Problem”

Bob, June 5th, 2006 at 2:50 pm

Lose. Not Loose. Lose.

Steh, June 9th, 2006 at 4:04 pm

I always wonder what they write when they actually mean “loose”.

Alternate, June 9th, 2006 at 6:30 pm

“THEY” ???

Sheesh, all this fuss over a typo? OKAY. I get the message. I’ll fix it whenever I have time.

Tar Martel, August 20th, 2006 at 3:13 am

Thanks for the simulation.
The first reaction I had when I read about the riddle, was to create a simulation… but then I found your JavaScript one. It is simple and to the point, I like it and it saved me the effort.

The solution:
The results are not so obvious when using the constraints of the riddle (three doors) but bump it up to a 100 doors and the odds are completely in your favour to change your mind, but then bump it up to a 1000 doors, and I’m afraid your simulation stops working. – More often than not you stand a 100% chance of loosing if you decide not to swap. I don’t believe those odds. Mathematically yes, but in my heart, no.

See my expanded comments at http://journal-of-a-novel.blogspot.com/2006/08/monty-hall-problem.html

I don’t believe that the solution is completely correct; I believe that humans can influence the results of this simple test in a way that cannot be mathematically calculated.

Regards,
Tar

Alternate, August 20th, 2006 at 3:48 pm

Hi Tar,

Thanks for the reply. and I do appreciate your link!

You’re absolutely right that humans, specifically the game show host, can influence the results of this test - simply by not following the rules.

On the actual game show, there’s no guarantee that the host will open a door. Which means that the host could choose to open a door only if the player has chosen the winning door - in which case the chances of winning by switching when the game show host opens a door, would be 0% !

ie, for a mathematical simulation to be accurate, the game show host should ALWAYS exhibit consistent behavior – he should ALWAYS be required to open a door.

Will, September 3rd, 2006 at 1:57 pm

Your simulator grossly inflates the odds of winning after switching doors in the case where the number of doors is greater than 3 because it counts any case in which you lose by not switching as one in which you win by switching, when in fact you still have to choose a door to switch to. This explains the obviously false results for a very large number of doors where you win 100% of the time by switching according to the simulation.

Alternate, September 3rd, 2006 at 5:30 pm

I appreciate your taking the time to post. Let me assure that there’s nothing wrong with the simulation.

Think of it this way:
3 doors:
==========

33.33 % chance to win by sticking to original choice,
66.66% chance to win by switching.
Simulate 1000 attempts, chances are you’ll win 333 - 334 or so times for sticking, 666 or so times for switching,

4 doors:
==========

25% chance to to win by sticking to the original choice,
75% chance to win by switching.
simulate 1000 attempts, chances are you’ll win 250 or so times for sticking, 750 or so times for switching.

1000 doors:
==========

.1% chance to win by sticking,
99.9 % chance to win by switching.

ie, Simulate 1000 attempts, chances are you’ll will 1 time for sticking, 999 times for switching. If you keep running the simulator repeatedly, every so often, you’ll win 2 times out of thousand, and every so often, you won’t even win once.

10000 doors:

.01% chance to win by sticking,
99.99 % chance to win by switching.
What that means is that when you run a 10000 door simulation, and you have only 1000 attempts, you only have 10% chance of winning even ONCE in those 1000 iterations.

IE, if you run the simulator 10 times with those parameters, chances are that only one time out of those 10 attempts, the simulator will say: one 1 time out of 1000!

What I’m trying to say, is that in order to be assured of even reasonably interpretable results, you will have to run the simulator for atleast as many attempts(iterations) as doors. And the more iterations, the better.

If you’re running the simulator for 1000 doors, bump up the number of iterations to atleast 10000.

Hmm, I should probably put this into the post. :P

Phillip, September 26th, 2006 at 11:24 am

Since you know in advance that one door, a door with the 2nd goat will always be eliminated, you are actualy picking one of two dors and thus a 50/50 chance. The eliminated 2nd goat is a pantom choice that never realy existed.

Alternate, October 8th, 2006 at 8:04 am

??? But there is a chance that you would pick the “second” goat in your attempt, then the host would eliminate the “first” goat.

Put simply here are the three possibilities:
1. you pick the “first” goat, host eliminates the “second” goat. (win if switch)

2. you pick the “second” goat, host eliminates the “first” goat. (win if switch)

3. you pick the car, Host eliminates either
“first” or “second” goats based on his discretion. (loose if switch)

Therefore, there’s a 2/3 chance of winning by switching.

Leave a Reply