• About
  • Contact
  • What People Are Saying

The Monty Hall Problem

Mar5th
2012
Leave a Comment Written by Daniel Veazey

Have you ever heard of the Monty Hall Problem? My friend Korgan, who runs the blog Better Than Granite, told me about it. It goes like this:

You are on the show Let’s Make a Deal, and you are presented with three doors. Behind one door is a car. Behind the other two doors are goats. You are asked to choose a door, but at this point, it remains closed. Monty Hall (the host of the show) knows what is behind each door, and opens up one of the other two doors to reveal a goat. Now you are given the option of switching to the other unopened door or staying with your original choice. What should you do?

Think about it for a minute.

When you first look at the problem, it seems like it doesn’t matter if you switch or not, because behind one of the remaining doors is the car and the other door, a goat. So you have a 50/50 chance of winning. But that’s wrong. Let’s look at the probabilities of the two strategies, not switching and switching:

Not switching

When you first choose a door, you have about a 33.3% chance of choosing the car, and about a 66.7% chance of choosing a goat. Now if you decide not to switch to the other door, it doesn’t matter which door Monty Hall opens, and your chances of winning the car remain at about 33.3%, and the chance that you’ve chosen a goat remain at about 66.7%.

Switching

Now let’s look at what happens when you decide to switch doors. If you chose the correct door the first time (about a 33.3% chance), and Monty Hall opens one of the other doors to reveal a goat, then you decide to switch, you’ll obviously get the other goat. So however many times you play this game, roughly 33.3% of the time you will end up with a goat if you always switch doors. But say you chose a door with a goat behind it first (about a 66.7% chance), once Monty reveals the other goat, and you always switch, you will end up with the car. So about 66.7% of the times you play this game and choose to switch, you will win the car.

The proof

It took me a little while to wrap my head around it, and since it was not very intuitive to me, I decided to write a Python program to test it. I wrote it so it would run through the game 100,000 times and report the results. It pseudo-randomly places a car behind one of the three doors and then chooses a door. One of the remaining doors with a goat is revealed and then the program checks to see if switching is favorable. It does all this without any visual output, but rather just tallies up the wins and losses and reports those. I was very pleased that it only took 22 lines of code. Here it is:

monty_hall_problem.py   
import random
def populate_doors(): # put a car behind one door
	door=['goat', 'goat', 'goat']
	door[random.randint(0,2)]='car'
	return door
wins = 0
losses = 0
# playing the game 100,000 times:
for x in range(100000):
	doors=populate_doors()
	first_choice=random.randint(0,2) # choose a random door
	for y in range(3): # reveal first losing, unchosen door
		if doors[y] != 'car' and y != first_choice:
			doors[y] = 'out'
			break
	if doors[first_choice] == 'car':
		losses = losses + 1 # contestant switched to losing door
	else:
		wins = wins + 1 # contestant switched to winning door
print "All choices were switched."
print "Wins:", wins
print "Losses:", losses

Feel free to test it out. You’ll find that roughly 66.7% of the time, the computer wins using this strategy. Korgan wrote a similar program in C. Here is his code:

monty_hall.c   
//evidence of the monty hall solution.
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define GAMES 3000000
 
int main(void){
    unsigned i, j, k, choice, winsbyswitch=0, door[3];
 
    srand(time(NULL));                                                          //initialize random seed.
    for(i=0; i<GAMES; i++){
        door[0] = (!(rand()%2)) ? 1: 0;                                         //give door 1 either a car or a goat randomly.
        if(door[0]) door[1]=door[2]=0;                                          //if 1st door has car, give other doors goats.
        else{ door[1] = (!(rand()%2)) ? 1: 0; door[2] = (!door[1]) ? 1: 0; }    //else, give 2nd door car or goat, give 3rd door what's left.
        choice = rand()%3;                                                      //choose a random door.
 
        //if the next door has a goat, and the following door has a car, or vice versa, you'd win if you switch.
        if(((!(door[((choice+1)%3)])) && (door[((choice+2)%3)])) || (!(door[((choice+2)%3)]) && (door[((choice+1)%3)]))) winsbyswitch++;
    }
    printf("\nAfter %u games, I won %u by switching.  That is %f%%. ", GAMES, winsbyswitch, (float)winsbyswitch*100.0/(float)i);
}
Be Sociable, Share!
  • Tweet

Related posts:

  1. Working on the bike problem with Python
  2. A PyPy experiment
Python    monty hall problem, program, Python
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
← Changing stuff in Geany
Calculating and testing probability with Python →

No Comments Yet

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why you should leave a comment

1. I read all the comments.
2. I reply and answer every question.
3. I always click through to see the sites of commentators.
4. Because you can.
5. You become a participant instead of just an observer.

EvoLve theme by Theme4Press  •  Powered by WordPress Daniel Veazey
Things I find interesting