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

A laptop with PHP code on the screen

A look at some of PHP 7.4’s new features

PHP 7.4 was released on 28 November 2019 and with it comes a lot of new features. In this post we'll examine a few of the more interesting ones along with examples of how they can be used. PHP has long been the punchline in low-effort memes, and though some of the disdain for the language is justified, more recent… Continue reading »

Some text from a computer terminal

Scheduling database backups with cron jobs

We're going to create a bash script which runs mysqldump (a MySQL backup program) to generate a database backup with a filename containing the date. We're then going to set up a cron jobĀ (a utility for running scheduled tasks) to run the script at regular intervals. We'll do all of this securely so that other users on the server can't… Continue reading »