> /dev/null

Interesting hacks, snippets, and shortcuts.

Mar 31

The Monty Hall problem

The Monty Hall problem is a somewhat counterintuitive probability problem loosely modeled after the game show "Let's make a deal". Here's the setup:

You're presented with 3 doors. Behind one of the doors is a brand new car. Behind each of the other two doors is a goat. You start by choosing a door. The host, (who knows whats behind each door) then opens a different door to reveal a goat. You are now given the opportunity to switch doors. If the door you pick has the car behind it, you win and get to keep the car. What's your best bet for winning the car?

At first glance, it seems like this game is 50/50 because there are two doors and you're choosing one of them. As it turns out though, if you switch doors you have a much better chance of winning the car!

Sticking with the door you picked before gives you a 1 in three chance of winning the car, switching to the final door gives you a 2/3 chance.

Why is this?

When you first chose, you had a 2/3 chance of picking a goat. When the host chose, he had a 100% chance of picking the goat. The probability of both of you having chosen goats is therefore 2/3 * 1 or 2/3. If there is a 2/3 chance that we have found both goats, there must also be a 2/3 chance that the final door is the car!

Still need some convincing?

Here is a ruby "simulator" you can try. It's simply the problem stated procedurally, so there is no room for trickery:

def play
  doors = [:goat, :goat, :car].shuffle
  choice = (rand * 3).to_i
  reveal = doors.each_with_index.find_index { |item, door| item == :goat && door != choice }
  flip = ([0,1,2] - [choice, reveal]).first

Here are my results running that a million times:

counts = {goat: 0, car: 0}
1000000.times do
  counts[play] +=1
puts counts.inspect # => {:goat=>333905, :car=>666095}