The Monty Hall Problem

← Back
closed doors

The Monty Hall problem concerns the change in probability when reducing the number of wrong answers to a question. The problem gets its name from a game show hosted by Monty Hall in which contestants were tasked with guessing which door a prize was behind. In this game there are 3 doors, behind two of which are goats and the other a car. The doors are identical and the prize is behind a random door so the odds of picking the correct door, as anyone could tell you, are 1 in 3.

What happens next is what makes the problem interesting, and where most people’s intuitions about probability lead them to make the wrong decision. Our host, the aforementioned Monty Hall, opens one of the 2 doors that the contestant didn’t pick to reveal a goat. The contestant is then faced with what has become known as the Monty Hall problem: they’re asked whether they wish to stick with the door they originally chose or to switch and choose the other remaining door.

At this point most people assume that, since there are 2 doors remaining, the probability of the prize being behind either door is 1 in 2. It may surprise you to learn that this assumption is incorrect. In fact, the intuition is so strong for many people that even after I’ve explained why it’s wrong you still may not believe me.

The answer lies in the fact that Monty Hall knows which door the prize is behind. When he opens the door he has to choose one that has a goat behind it. If you happen to have picked a door with a goat behind first (which should happen 2 out of 3 times) then Monty is left with only one door he can open to reveal a goat. This means that 2 out of 3 times the remaining door will be the one with the prize, compared to the 1/3 chance of the door the contestant originally picked being the correct one.

I found this problem very counter-intuitive, and developing it as a program helped me to understand it better.

Pseudocode:

// Random number between 1 and 3 is assigned to variable prize
// Contestant picks a door which is assigned to variable choice
// Open one of the remaining doors that doesn't have a prize
if (choice === prize) {
  // Randomly remove one of the other 2 doors
} else {
  // Remove the door that wasn't picked and isn't the prize
  // Contestant chooses to stick or switch
}

// Determine the result
if (choice === prize) {
  // Contestant has won
} else {
  // Contestant has lost
}

We can see that at line 7, unless the contestant has chosen the prize on their first guess, the only door that can be removed is the door that doesn’t have the prize. That means that the remaining door the contestant didn’t choose originally will have the prize behind.

Recent Blog Posts


How to improve your Google PageSpeed score with Webp images

If you're still serving images in long-time standard formats such as JPEG and PNG then a Google PageSpeed Insights analysis of your site will identify an opportunity to improve your page loading times by serving images in "next-gen" formats. There are several so-called "next-gen" image formats which offer superior compression to JPEG and PNG but in this post we will… Continue reading »

A puzzle with one piece missing

What are WebSockets?

What do WebSockets do? Hypertext Transfer Protocol (HTTP) is unidrectional, meaning requests can only be made by the client. This is a limitation which means that a certain class of web application cannot be built using HTTP alone. WebSockets are a separate communication protocol, fully compatible with HTTP, which allow for bidirectional communication between a client and a server. A protocol… Continue reading »