The Monty Hall Problem (Part 2)

If you are unfamiliar with The Monty Hall Problem please read the first part of this blog post here.The monty hall problem program

Due to my interest in programming and interest in this problem I decided to bring the two together and created a program that replicates the situation and outputs the statistics of wins and losses with switches and without (where the results can be seen further down).

Computing the Problem

I have uploaded the source code to the program which can be found here.

The program works by creating 3 doors at the start of each game and randomly choosing 1 to be the winner. The program then asks the user to pick one of the doors either 1, 2 or 3, after this it will then display one of the other doors (if the user has picked the winning door it will randomly choose one of the other 2 doors to show). The user is then given the choice to switch the door if they want to. The program then tells the user if they won or lost and prints out all the statistics of the total games which can be viewed below.

To make it easier to quickly run the program multiple times I created short java program to produce a file that can then be fed into the standard input of the main file to run the game 1000’s of time quickly and view the results. That program can be found here.

The Results

I ran the program multiple times using the file printing program to give me a different number of inputs each time from 20 games to 20,000 here you can see the screenshots of the results below.

Results of 10 switches and 10 none switches.
Results of 10 switches and 10 none switches.
Results of 100 switches and 100 none switches.
Results of 100 switches and 100 none switches.
Results of 1,000 switches and 1,000 none switches.
Results of 1,000 switches and 1,000 none switches.
Results of 10,000 switches and 10,000 none switches.
Results of 10,000 switches and 10,000 none switches.

The main point of interest of these results is the percentages, and for anyone who wasn’t convinced of the solution to the problem these results clearly show that you have a greater chance of winning if you change from your original choice (as you’d expect). Also, we can see that the more games that are played the closer we get to the values of 1/3 (wins without switching) and 2/3 (wins with switching).

The Monty Hall Problem (Part 1)

This part of the post is about explaining the problem itself and why the answer is what it is, whilst the second part is about computing the answer and “proving” it. Therefore, if you are already familiar with the problem skip to part 2 of the post.

Since reading about the problem in a book when I was young, the monty hall problem, has always been something I have found interesting, and found myself on the Wikipedia page for it multiple times (including when it was used in one of Derren Browns shows, and in the movie 21). So I decided to write a program that demonstrates it and write a blog post about it.

The “Problem”

Imagine you are on a game show and the host shows you 3 closed doors and asks you to pick one – either 1, 2 or 3. You are told that behind 2 of the doors there is a goat, but behind one of the doors is a grand prize of a car. After you have made your choice, let’s say you choose door number 1, the host (knowing which doors contain the goats) will then open one of the doors you haven’t picked to reveal a goat. For this example let’s say he shows you door number 2. After, showing you the goat behind this door, you are then given the option to swap your choice to the remaining door if you wish.

The question is, do you have a better chance of winning if you stick to your original choice (door 1) or are you more likely to win if you switch (to door 3)?
Or worded differently:
What are the chances of the car being behind door number 1, and the chances of it being behind door number 3?

The Solution

It seems like the answer is obvious – there is a 50% chance of winning whether you stick with your choice or if you switch, and therefore switching has no benefit. This answer is actually incorrect, despite it seeming so simple and clear.

The fact is you actually have 1 in 3 (33.33%) of chance of winning if you stick with your initial answer BUT if you decide to switch your chances of winning increase to 2/3 (66.66%).


The main reason the chance of winning changes from 1/3 to 2/3 is because the host has to choose one of the losing doors.
On my first choice there is 1 in 3 chance of picking the grand prize, and a 2  in 3 chance of picking a goat.
If I pick a goat on my first choice (which is more likely), the host will then show the door which doesn’t have the car behind it, meaning when I switch I will win the car.

The wikipedia page does a nice job of explaining why the odds change, or if you prefer here is a youtube video which explains why (Skip to 2m 40s for the explanation of why):

The second part of this post can be found here where I discuss the program I created based on the problem and the results it produces.