Introduction
The Monty Hall Problem is a puzzle named for Mr. Monty Hall, a game show host from the 60's and 70's. The problem can be summed up as follows: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?
In the video above there is detail on how to intuitively find the answer where you should always switch. Most people's intuitive answer is that after Monty Hall shows you the second door, you have a 50% chance of finding the sports car. While this is true (given two doors, one car), this way of looking at the problem ignores the fact that Monty Hall just revealed a goat to us. Remember that you had a 2/3 chance of getting a goat on your first pick. The following table I believe demonstrates the choices well:
Car in Door 1

Car in Door 2

Car in Door 3

Result in picking door #1 and not switching

Result if
switching

Car

Goat

Goat

Car

Goat

Goat

Car

Goat

Goat

Car

Goat

Goat

Car

Goat

Car

The program test
Hypothesis
Given enough iterations, the success rate of switching should tend to 66% given 3 doors.
Pseudo codedefine Monty Hall Simulation(runs = 10000, random_seed = None):
"""Runs a simulation of a series of monty hall games. 'runs=' is the variable to
determine the number of times you run the test. The program will run 3 cases, always
switch ("theoretical" optimal solution), Never switching(suboptimal solution), and
Sometimes switching (ignoring the first bit of information in the monty hall problem).
"""
random.seed(random_seed)
game = Game Show class
always_switch, never_switch, sometimes_switch = [0, 0] # a list form that keeps track of wins and losses
for i in range(runs):
# always switch 
j = game.play(strategy = 0)
if j is True:
always_switch[0] += 1
else:
always_switch[1] += 1
# never switch 
k = game.play(strategy = 1)
if k is True:
never_switch[0] += 1
else:
never_switch[1] += 1
# sometimes switch 
l = game.play(strategy = 2)
if l is True:
sometimes_switch[0] += 1
else:
sometimes_switch[1] += 1
# Print Results 
print(results)
The Monty Hall class
class GameShow:
define Initialization(self):
"""Initializes GameShow to a new instance"""
doors = [0, 0, 0]
first_choice = None
second_choice = None
reveal = None
define play(self, strategy=0):
"""Strategy is how one plays the game
0 is always switch to the other door
1 is never switch (first choice is second choice)
2 is sometimes switch (random choice on second choice)
Any other value will give the 0 strategy"""
set_up()
first_pick()
reveal()
if strategy is 0:
second_pick(True)
else if strategy is 1:
second_pick(False)
else if strategy is 2:
s = random.choice([True, False])
second_pick(s)
return self.is_win()
define set_up(self, car=None):
"""resets the game"""
define first_pick(self, pick=None):
"""picks a first pick"""
self._first_choice= random.randint(0, 2)
define reveal(self):
"""Game show host reveals a goat"""
define second_pick(self, switch=True):
"""user picks a new door"""
if switch is True:
second_choice = 3  (first_choice + reveal)
else:
second_choice = first_choice
define is_win(self):
"""returns the win state"""
Results
The following table was the output of the python script. as can be seen below, the percentage to 3 significant figures pans out to 66% victory rate for always switching, 33% for sticking to your guns, and an even 5050 for randomly switching.
Runs: 10000Time: 0.72 seconds
Seed: 1
==========================
Always Switch:
Win: 6673
Loss: 3327
Percentage: 66 %
==========================
Never Switch:
Win: 3343
Loss: 6657
Percentage: 33 %
==========================
Sometimes Switch:
Win: 5041
Loss: 4959
Percentage: 50 %
==========================