The Monty Hall Puzzle

You’ve all heard this puzzle. Remember the game show “Let’s Make a Deal”, there are three closed doors and behind one is a prize. The contestant picks a door, then the host – Monty Hall – opens a different door. The prize is not behind the door Monty opens. Now the contestant has an option to stick to the original choice or switch. Should you switch or not?

Turns out the answer is that you should always switch. In fact the probability of winning the prize is 2/3 if you switch.

Many people have hard time understanding why this is so. Once I explained the solution to a friend and he still did not believe me. So I wrote the following program to simulate 10,000 games and calculate the probability. Here is the code in Python:

from random import randint
def pick (doors):
        result = randint (1,3)
        while result in doors:
                result = randint (1,3)
        return result
total_wins = 0
for i in range (1,10000):
        prize_door = randint (1,3)
        guess_door = randint (1,3)
        monty_door = pick ([prize_door, guess_door])
        switched_door = pick ([guess_door, monty_door])
        if (switched_door == prize_door):
                total_wins += 1
print “Total games: %d Total wins: % d ” % (i, total_wins)
When I run this program here are several results:
Total games: 9999 Total wins:  6700
Total games: 9999 Total wins:  6675
Total games: 9999 Total wins:  6619
So, as you can see the probability of winning is about 2/3rds….
I rest my case. 🙂
Advertisements

2 thoughts on “The Monty Hall Puzzle

  1. I don’t really know Python, so I might be mistaken, but it looks to me like the pick() function is horribly inefficient. You could do better than that!! 🙂

    • You are correct. I wouldn’t call it “horrible” though. Although there is a non-zero probability that the function will run forever – since it is picking a random number not in a list. However, on average the performance is sufficiently fast… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s