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 randintdef pick (doors):result = randint (1,3)while result in doors:result = randint (1,3)return resulttotal_wins = 0for 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 += 1print “Total games: %d Total wins: % d ” % (i, total_wins)

When I run this program here are several results:

Total games: 9999 Total wins: 6700Total games: 9999 Total wins: 6675Total games: 9999 Total wins: 6619

So, as you can see the probability of winning is about 2/3rds….

I rest my case. 🙂

Advertisements

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… 🙂