Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / Python
Tip/Trick

Unveiling the Counterintuitive Nature of the Monty Hall Problem - A Simulation-Based Investigation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
13 Jun 2023CPOL5 min read 5.7K   3   9
This post delves into the perplexing Monty Hall paradox, examining the probabilities associated with sticking or switching doors in the game scenario.
This study discusses the perplexing Monty Hall paradox, a probability puzzle that has fascinated mathematicians and puzzle enthusiasts for decades. Through a rigorous simulation-based approach, the research examines the probabilities associated with sticking or switching doors in the game scenario. By providing empirical evidence and challenging common misconceptions, this investigation sheds light on the counterintuitive nature of the Monty Hall problem, offering valuable insights into conditional probability and decision-making.

Introduction

The Monty Hall paradox is a well-known probability puzzle that has fascinated mathematicians, statisticians, and puzzle enthusiasts for decades. Named after the host of the popular game show “Let’s Make a Deal”, Monty Hall, this paradox challenges our intuition and reveals the surprising nature of probability.

In the Monty Hall problem, a contestant is faced with three doors, behind one of which lies a valuable prize, such as a car, while the other two doors conceal less desirable items, typically represented by goats. The contestant’s objective is to select the door that conceals the coveted prize.

The twist in the game comes when the host, Monty Hall, who possesses knowledge of the door contents, intervenes by opening one of the remaining doors, revealing a goat. At this point, the contestant is presented with a crucial decision: stick with their initial choice or switch to the other unopened door.

At first glance, the Monty Hall problem may appear deceptively simple, leading many to assume that the contestant’s chances of winning are equally likely regardless of their choice to stick or switch. However, this assumption is incorrect and forms the crux of the paradox. The counterintuitive result lies in understanding the implications of conditional probability and the host’s intentional revelation of information.

Numerous individuals, including mathematicians and those with statistical backgrounds, have fallen victim to the common misconception that the contestant’s odds remain at 1/2 after Monty Hall opens a door. This incorrect belief arises from the assumption that the host’s action does not provide any new information regarding the location of the prize.

By delving into the world of probability and conditional reasoning, we hope to shed light on the fascinating paradox and contribute to the understanding of the puzzle that has captivated minds and sparked intriguing discussions among both experts and enthusiasts alike.

Simulation

To conduct the simulation, we will be using Python and the built-in random library. Specifically, we will utilize the randrange function, which generates a random integer within a specified range. In our case, we want to generate a random integer from 0 to 2, so we can use randrange(3).

Python
from random import randrange

Next, we need to define the number of iterations to simulate the game (iters) and initialize variables (swapped_wins and unswapped_wins) to keep track of the occurrences of winning through swapping or not swapping.

Python
# Number of iterations to simulate
iters = 1_000
# Number of wins from swapping and not swapping
swapped_wins = 0
unswapped_wins = 0

Now, we will use a for loop to iterate a specified number of times (iters). Inside the loop, we create a list called doors to represent the values behind each door. Initially, all doors are set to False (indicating goats), and we will randomly assign the location of the car later. Additionally, we initialize three variables (first, opened, and final) to hold the indices of the first chosen door, the door opened by the host, and the final chosen door, respectively.

Python
for i in range(iters):
    # Starts as all goats (will choose
    # car placement later)
    doors = [False, False, False]
    # Initialise variables for door choices
    first = 0
    opened = 0
    final = 0

Inside the loop, we randomly assign the location of the car. By generating a random index, we can mark the corresponding door as True, indicating it holds the car.

Python
# Randomly assign car
car = randrange(3)
doors[car] = True

In the game, the contestant is initially allowed to choose one of the three doors. We can use the following line to randomly select a door.

Python
# Initially pick at random
first = randrange(3)

At this point, the host opens one of the doors that contains a goat (False). To achieve this, we iterate through the values in the doors list and find the first door that hasn’t been picked by the contestant and is a goat. Once we identify this door, we set its value to None to indicate it has been opened, and we assign the index of the opened door to the variable opened.

Python
# Remove one of the goat doors
for j in range(len(doors)):
    if doors[j] == False and j != first:
        doors[j] = None
        opened = j
        break

Next, the contestant is given the opportunity to swap their chosen door. Since only one door is available to swap to (out of the remaining two doors, with one already chosen), the code below identifies the only possible door and assigns it to the variable final.

Python
# Try swapping door
for j in range(len(doors)):
    if j != first and j != opened:
        final = j

As the simulation represents the perspective of the host, we can observe both outcomes: winning through swapping and winning through not swapping. We check if the door marked as the final choice contains the car (True) and, if so, increment the swapped_wins counter.

Python
if doors[final] == True:
    swapped_wins += 1

We can also determine if the contestant would have won without swapping their original choice and, if so, increment the unswapped_wins counter.

Python
if doors[first] == True:
    unswapped_wins += 1

Now, outside of the loop, we can calculate the number of wins through swapping and not swapping. Consequently, we can calculate the percentage probabilities of winning through swapping or not swapping.

Python
print("Swapped wins: ", swapped_wins)
print("Prob is", swapped_wins / iters)
print("Unswapped wins:", unswapped_wins)
print("Prob is", unswapped_wins / iters)

Results

After running the script with 1 million iterations (iters = 1_000_000), the following output was obtained:

Swapped wins is 666916
Prob is 0.666916
Unswapped wins is 333084
Prob is 0.333084

As demonstrated, these results support the hypothesis that the probability of winning increases to approximately 2/3 when swapping and remains around 1/3 when not swapping.

To further confirm the results, the script was rerun with 1 trillion iterations (iters = 1_000_000_000). After some time, the following output was obtained:

Swapped wins is 666669495
Prob is 0.666669495
Unswapped wins is 333330505
Prob is 0.333330505

At around the 1 trillion mark, we obtain results that align closely with the theoretical probabilities of 2/3 and 1/3, respectively, up to five decimal places.

Conclusion

The simulation of the Monty Hall problem, conducted through iterative iterations and analyzing the resulting outcomes, provides empirical evidence supporting the probabilistic advantage of switching doors. By employing conditional probability and utilizing the law of total probability, we observe that swapping doors after the host reveals a goat increases the contestant’s chances of winning the car.

The probability of initially selecting the correct door is 1/3, as there are three equally likely options. However, after the host reveals a goat behind one of the remaining doors, the contestant’s knowledge is updated, leading to a conditional probability shift. Applying the law of total probability, we recognize that the probability of the car being behind the unopened door, given the host’s revealed information, is 2/3.

Through extensive simulation iterations, the results consistently converge to a winning probability of approximately 2/3 (0.666) when the contestant switches doors, while the probability of winning by not switching remains around 1/3 (0.333). This empirical evidence aligns with the theoretical analysis and confirms the counterintuitive nature of the Monty Hall problem.

Therefore, based on mathematical analysis, the simulation supports the conclusion that swapping doors after the host reveals a goat offers a higher probability of winning the car compared to sticking with the initial choice. The application of conditional probability and the law of total probability elucidates the underlying probabilistic advantage of switching, demonstrating the validity of the Monty Hall paradox.

History

  • 13th June, 2023: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
United Kingdom United Kingdom
I like programming and maths

Comments and Discussions

 
QuestionGreat explanation ... Pin
Richard MacCutchan6-Oct-23 5:56
mveRichard MacCutchan6-Oct-23 5:56 
QuestionSame in Visual Basic Pin
HenkAlles18-Jun-23 23:40
HenkAlles18-Jun-23 23:40 
GeneralFor those who do not like Python but love R Pin
HenkAlles18-Jun-23 23:52
HenkAlles18-Jun-23 23:52 
QuestionIs it a Paradox? Pin
George Swan13-Jun-23 11:00
mveGeorge Swan13-Jun-23 11:00 
AnswerRe: Is it a Paradox? Pin
Jack Devey13-Jun-23 15:06
Jack Devey13-Jun-23 15:06 
GeneralRe: Is it a Paradox? Pin
PIEBALDconsult13-Jun-23 14:18
mvePIEBALDconsult13-Jun-23 14:18 
GeneralRe: Is it a Paradox? Pin
BI PL16-Jun-23 6:15
BI PL16-Jun-23 6:15 
GeneralRe: Is it a Paradox? Pin
ZedForLife22-Jun-23 14:50
ZedForLife22-Jun-23 14:50 
GeneralRe: Is it a Paradox? Pin
BI PL23-Jun-23 20:06
BI PL23-Jun-23 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.