> cat projects/monty-hall-simulator.hy

README.md

Monty Hall Simulator

A Python project exploring the Monty Hall problem through an interactive game, simulation, and statistical analysis.

I originally built the core game and simulator as a programming exercise to test my understanding of the Monty Hall problem. The project later expanded into a broader analysis of how switching, the number of doors, the number of doors revealed, and the host’s knowledge affect the probability of winning.

The Monty Hall Problem

In the classic version of the problem:

  1. There are three doors.
  2. One door hides a prize and the other two are losing doors.
  3. The player chooses a door.
  4. Monty, who knows where the prize is, opens one of the other doors and always reveals a losing door.
  5. The player can keep the original choice or switch to the remaining unopened door.

Keeping the original choice wins about 1/3 of the time, while switching wins about 2/3 of the time.

This project starts with that classic problem and then explores what happens when those assumptions are changed.

Project Structure

.
├── game.py
├── simulator.py
├── analysis.ipynb
├── analysis.html
├── results.csv
├── requirements.txt
└── README.md

game.py

An interactive command-line implementation of the classic three-door Monty Hall game.

The player chooses a door, Monty reveals a losing door, and the player decides whether to stay or switch.

simulator.py

A command-line simulator for running Monty Hall experiments at larger scales.

The simulator supports configurable:

  • numbers of trials,
  • numbers of doors,
  • numbers of doors revealed,
  • informed or uninformed host behavior,
  • random seeds for reproducibility,
  • and CSV output files.

analysis.ipynb

A Jupyter notebook containing the extended statistical analysis and visualizations.

The notebook explores:

  • the classic three-door problem,
  • observed versus theoretical win rates,
  • cumulative wins and convergence,
  • different simulation sample sizes,
  • repeated batches and sampling variability,
  • 95% confidence intervals,
  • generalized games with more doors,
  • different numbers of doors revealed,
  • switching advantage as more information is revealed,
  • strategy comparisons,
  • simulation error versus theoretical probability,
  • informed versus uninformed hosts,
  • and cases where an uninformed host accidentally reveals the prize.

analysis.html

A rendered version of the notebook that can be viewed in a browser without running Jupyter.

results.csv

Example simulation output generated by simulator.py.

An Important Generalization

The classic Monty Hall result depends on more than simply opening a door.

If there are N doors and an informed Monty reveals R losing doors, a player who switches randomly among the remaining unopened alternatives has theoretical win probability

P(switch) = (N - 1) / [N × (N - 1 - R)]

This makes it possible to study how the value of switching changes as Monty reveals more information.

For example, with 100 doors:

  • if Monty reveals only one losing door, randomly switching provides almost no advantage over staying;
  • if Monty reveals 98 losing doors and leaves only one alternative, switching wins about 99% of the time.

Informed vs. Uninformed Monty

The analysis also considers a different host policy.

An informed host knows where the prize is and deliberately avoids revealing it. This is the assumption used in the classic Monty Hall problem.

An uninformed host reveals doors without knowing where the prize is. In this version, the host can accidentally reveal the winning door.

This distinction demonstrates an important part of the problem: the information contained in Monty’s actions depends on the rules governing his behavior.

In the classic three-door game:

Host behavior Stay Switch


Informed Monty 33.3% 66.7% Uninformed host, counting all rounds 33.3% 33.3% Uninformed host, conditional on revealing a losing door 50% 50%

The switching advantage is therefore not caused merely by the physical act of opening another door.

Installation

Clone the repository and create a virtual environment:

git clone https://github.com/Hyaxon/monty-hall-simulator
cd monty-hall-simulator

python -m venv .venv
source .venv/bin/activate

On Windows:

.venv\Scripts\activate

Install the dependencies:

python -m pip install -r requirements.txt

Running the Interactive Game

python game.py

Follow the prompts to choose a door and decide whether to stay or switch.

Running the Simulator

A basic simulation:

python simulator.py --games 100000 --doors 3 --reveals 1 --host informed --seed 42

A 100-door game where Monty reveals only one losing door:

python simulator.py \
    --games 100000 \
    --doors 100 \
    --reveals 1 \
    --host informed \
    --seed 42 \
    --output one_reveal.csv

A 100-door generalized game where Monty reveals as many losing doors as possible:

python simulator.py \
    --games 100000 \
    --doors 100 \
    --reveals max \
    --host informed \
    --seed 42 \
    --output max_reveal.csv

An uninformed host who may accidentally reveal the prize:

python simulator.py \
    --games 100000 \
    --doors 10 \
    --reveals 3 \
    --host uninformed \
    --seed 42 \
    --output uninformed.csv

Simulator Options

Option Description


--games Number of simulated trials --doors Total number of doors --reveals Number of doors revealed, or max --host informed or uninformed --seed Optional random seed for reproducible results --output CSV output path

Running the Analysis Notebook

Start Jupyter:

jupyter notebook monty_hall_full_analysis.ipynb

The notebook performs its own simulations, so a CSV generated by simulator.py is not required.

If you only want to read the analysis, open monty_hall_full_analysis.html in a browser.

What I Learned

This project began as practice implementing a probability problem in Python and grew into an experiment in simulation and statistical reasoning.

Some of the main ideas demonstrated by the project are:

  • simulation results become more stable as the number of trials increases;
  • switching is advantageous in the classic game because of the host’s informed reveal policy;
  • revealing more known losing doors can dramatically increase the value of switching;
  • revealing only a small amount of information can provide very little advantage in games with many doors;
  • and changing the host’s knowledge changes the probability model itself.

GenAI Acknowledgments

I wrote the core Monty Hall game and simulator as a programming exercise to test my understanding and develop a better intuition for the problem..

The extended analysis notebook, including its statistical analysis, visualizations, generalized experiments, and notebook code, was generated by OpenAI’s ChatGPT in response to my ideas, questions, and requests for additional experiments.

License

This project is licensed under the MIT License. See LICENSE for details.