Monty Hall Problem Simulation

Gaurav Kumar
3 min readSep 22, 2023

Create a Python program that simulates the Monty Hall problem.

In the following piece, I will guide you through the process of crafting a Python script that replicates the renowned Monty Hall problem. By doing so, we aim to definitively prove the correctness of the solution presented by Steve Selvin for this intriguing problem.

The Monty Hall problem is a classic brain teaser that has stumped many, even the brightest minds. Named after Monty Hall, the host of the TV game show “Let’s Make a Deal,” this problem seems deceptively simple at first glance. However, it’s known for its perplexing nature and ability to challenge our intuition. In this article, we’ll explore the Monty Hall problem through a simulation, making it easy to understand and appreciate.

The Monty Hall Problem Explained:

Imagine you’re a contestant on a game show. There are three doors in front of you. Behind one of these doors is a brand-new car, and behind the other two are goats. Your mission is to choose the door with the car behind it. You make your choice, selecting, let’s say, Door #1. At this point, you have a one in three chance of picking the car, right?

Now, here’s where it gets interesting. The host, Monty Hall, who knows what’s behind each door, opens one of the remaining two doors, revealing a goat. Let’s say he opens Door #2, revealing a goat. So, you’re left with your initial choice, Door #1, and the unopened Door #3.

Here’s the Monty Hall problem in a nutshell: Should you stick with your initial choice (Door #1) or switch to the other unopened door (Door #3) to maximize your chances of winning the car?

Simulation Time:

To understand this problem better, we can simulate it using Python. Let’s write a simple Python program to model the Monty Hall scenario and see if it’s indeed better to switch doors.

import random

def monty_hall_simulation():
# Initialize the doors with one car and two goats randomly placed behind them.
doors = ['car', 'goat', 'goat']
random.shuffle(doors)

# The contestant's initial choice.
initial_choice = random.choice([0, 1, 2])

# Monty Hall's reveal of a goat behind one of the unchosen doors.
for i in range(3):
if i != initial_choice and doors[i] == 'goat':
revealed_goat = i
break

# The contestant's decision to switch or stick.
switch = random.choice([True])

# Determine the final result.
final_choice = initial_choice if not switch else 3 - initial_choice - revealed_goat

return doors[final_choice] == 'car'

# Simulate the game multiple times to get the probabilities.
num_simulations = 10000
switch_wins = 0
stay_wins = 0

for _ in range(num_simulations):
if monty_hall_simulation():
switch_wins += 1
else:
stay_wins += 1

# Calculate and print the results.
switch_win_percentage = (switch_wins / num_simulations) * 100
stay_win_percentage = (stay_wins / num_simulations) * 100

print(f"Switching doors wins {switch_win_percentage:.2f}% of the time.")
print(f"Sticking with the initial choice wins {stay_win_percentage:.2f}% of the time.")

Output:
Switching doors wins 67.22% of the time.
Sticking with the initial choice wins 32.78% of the time.
probability with simulation

Conclusion:

After some wild swings in the initial couple of experiment, we can see that the probability naturally shifts towards 67% or two-thirds.

Running this Python simulation will show you that, indeed, it’s significantly better to switch doors in the Monty Hall problem. You’ll likely find that switching results in a higher probability of winning the car. This intriguing puzzle reminds us that sometimes, our intuition can lead us astray, and a bit of probabilistic thinking can go a long way in making optimal decisions. So, the next time you’re faced with a tricky choice, remember the Monty Hall problem and the power of probability!

You can add some more texture to this problem by changing the number of doors and Monty being more or less generous and opening different number of doors and running the simulation to see how the probability maps out.

Found this interesting, checkout my page to know more such interesting stuff.

Thanks and Happy learning!!!

--

--

Gaurav Kumar
Gaurav Kumar

No responses yet