There is a puzzle used in game shows known as The Monty Hall Problem. It's been around for a while but over lunch yesterday someone explained it to me for the first time and 3 out of 4 of us argued convincingly the same answer. And it was wrong. Here's the problem:

Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?

The most logical answer to me was no it doesn't matter if you switch or not because you never knew what was behind the first door you chose anyway. It seemed to me that the problem was no different than having two choices, a goat or a car and randomly choosing one. But this is all wrong!

Since I'm not good at math I could only loosely follow the explanations for why I was wrong. So, naturally, I wrote some code to see it in action (bear with me, I spent all of 5 minutes on it):

```
import random
from decimal import Decimal
choices = ['goat','goat','car']
tries = 99000
switch_correct, stay_correct = 0, 0
for num in range(tries):
doors = [c for c in choices]
random.shuffle(doors)
first = doors.pop(random.randint(0,len(doors)-1))
for i,val in enumerate(doors):
if val == 'goat':
doors.pop(i)
switched = doors[0]
if switched == 'car':
switch_correct += 1
elif first == 'car':
stay_correct += 1
print "stay: you win %s%% of the time" % (Decimal(stay_correct) / Decimal(tries) * 100)
print "switch: you win %s%% of the time" % (Decimal(switch_correct) / Decimal(tries) * 100)
```

I found the result astonishing:

stay: you win 33.5% of the time switch: you win 66.5% of the time

The wikipedia link above explains why this is but it is still incredible to me, like a magic trick.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Roberto Alsina on Wednesday Feb 13th, 2008 at 12:37p.m.

It's actually quite simple to understand.

Would you change your door for both the other doors without any being opened?

If you answer yes to that question (and you should ;-) it's obvious that since it's always possible to open one of the two doors it doesn't matter if the host actually opens one or not, and the game as explained is equivalent to the one I describe above.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Thomas Yager-Madden on Wednesday Feb 13th, 2008 at 1:18p.m.

The decision tree illustration on the wikipedia page is the line of thought that helped me finally get it (even though, as lunch proved, I'm still not especially good at conveying it to anyone else).

Another helpful way to reframe the problem, is think of it set up with 1000 doors, with still only one car. You would have a 1/1,000 chance of picking the correct door on the first. If Monty opens 998 doors, all of them with goats behind them, the door that you chose first will still have a 1/1,000 chance of being the one that conceals the car, but the other remaining door will have a 999/1,000 probability of being the door that is concealing the car.

Here, switching seems obviously a good idea. Even with just three doors, the principle in operation is the same: the probabilities must add up to one, and your first pick always leaves you with the worst chances.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Robin Munn on Wednesday Feb 13th, 2008 at 1:53p.m.

There's a simple way to look at it.

When you picked your initial door, there was a 1/3 chance you were right, and a 2/3 chance you were wrong.

The prize hasn't moved since the host opened one of the other doors. All that's changed is that *if* you were wrong about where the car was (remember, there's a 2/3 chance that you were wrong), then you now know where it is. In other words, there's a 2/3 chance that the car was behind *one* of those two other doors -- and of those two other doors, you know which one to pick because the host has helpfully eliminated the other one for you.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Kumar McMillan on Wednesday Feb 13th, 2008 at 2:12p.m.

thanks, I think I finally got it. Funny how "random" isn't so random after all when you run it 99,000 times. I think I was hung up on the fact that choosing one door then another are both random decisions.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Richard on Wednesday Feb 13th, 2008 at 5:04p.m.

Yes, it's an interesting problem. Your code is a little more complex than it needs to be though :)

The inner loop just needs to be:

doors = list(choices)

random.shuffle(doors)

first = doors.pop()

doors.remove('goat')

[I hope that makes it through your comment system OK :) ]

Also there's no need to involve Decimal: "stay_correct / float(tries) * 100)" will do the job

## Re: The Monty Hall Problem (win a goat or a car)

posted by Jussi Salmela on Thursday Feb 14th, 2008 at 4:53a.m.

I believe the for block just needs to be:

random.shuffle(choices)

if choices[random.randint(0, len(choices) - 1)] == 'car':

stay_correct += 1

else:

switch_correct += 1

At least this produces the same results as your code.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Kumar McMillan on Thursday Feb 14th, 2008 at 12:59p.m.

ah, nice, forgot about list.remove() :) ... and Jussi's is a nice simplification but it obfuscates the fact that a goat is revealed and the last door is chosen which would have hurt my brain if I'd coded it like that

## Re: The Monty Hall Problem (win a goat or a car)

posted by Matt Wilson on Thursday Feb 14th, 2008 at 5:25p.m.

Some people are more comfortable with abstractions. I'm a concrete thinker. Usually I prefer drawing out my own abstract theory once I see a lot of examples.

Neat illustration.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Judith on Saturday Feb 23rd, 2008 at 12:06a.m.

We learned about this in college, it seems funny to me now cuz at the time we had a whole section devoted to the mathematics of "game" shows. Anyways, understanding it well enough to write code is all that mathematicians hope to do with a proof anyways. Fun to rethink it! Thanks.

## Re: The Monty Hall Problem (win a goat or a car)

posted by Brett Bernstein on Monday Mar 10th, 2008 at 11:12p.m.

I remember getting a similar question like this during an interview a few years back but it was geared around the end of a basketball game and should you go for a 2 pt shot to tie (and go to OT) or go for a 3 pt shot to win.

For this one, I look at it as a 1/3 chance of being right on the first pass, and a 1/2 chance of being right after the first goat is revealed. Somewhere on my book shelf is an economic game theory book that explains how the math works out. In effect, you have two rounds of the game and different success probabilities for each round.