Skip to content

franckferman/Monty-Hall-Problem-Simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CI Contributors Forks Stargazers License Go Version Platform

Monty Hall Problem Simulator

Monty Hall Problem Simulator

Should you switch or stay? One million simulations later, the answer is still the same.

A probability simulator built around one of the most counterintuitive results in mathematics.
Interactive web interface and full-featured CLI, with the complete mathematical derivation.


Table of Contents

Click to collapse/expand
  1. The Problem
  2. The Mathematics
  3. Quick Start
  4. Web Interface
  5. Command-Line Interface
  6. Build from Source
  7. Contributing
  8. Star Evolution
  9. License
  10. Contact

The Problem

In 1963, a game show called Let's Make a Deal began airing on American television. Contestants faced three closed doors. Behind one was a car. Behind the other two, goats. The host β€” Monty Hall β€” always knew where the car was.

The game went like this: you pick a door. Monty then opens one of the other doors, always revealing a goat. He asks: do you want to switch to the remaining door, or stick with your first choice?

Most people say it doesn't matter. 50/50, right? Two doors left, one car. In 1990, Marilyn vos Savant published the correct answer in Parade magazine β€” always switch, you win twice as often β€” and received nearly 10,000 letters from mathematicians and academics telling her she was wrong.

She wasn't.

This simulator exists to make that result undeniable. Run it once with 10 trials and the noise might fool you. Run it a million times and the numbers converge exactly where the math says they should.


The Mathematics

Why intuition fails

When Monty opens a door, most people unconsciously apply what psychologists call the equal likelihood heuristic: two doors remain, so each must have a 50% chance. This reasoning is wrong because it ignores the nature of Monty's action.

Monty does not open a door at random. He has perfect knowledge and always reveals a goat. This constraint makes his action informative β€” he is not generating new uncertainty, he is removing one specific possibility in a way that is directly conditioned on where the car is. Once you account for that, the symmetry breaks.

Conditional probability

You pick door 1. Monty opens door 3 (a goat). Two cases:

  • Staying wins if and only if your initial pick was correct. That probability is 1/3. Nothing that happens afterwards changes that prior.
  • Switching wins if and only if your initial pick was wrong. That probability is 2/3.

This is the entire argument, and it requires no formula. If you picked wrong (probability 2/3), the car is behind one of the other two doors. Monty eliminates the one without the car. The remaining door therefore has the car. Switching wins.

Bayesian derivation

Define C_i as the event "car is behind door i". You have picked door 1. Monty opens door 3. We want P(C_2 | host opens door 3).

By Bayes' theorem:

P(C_2 | host opens 3) = P(host opens 3 | C_2) Γ— P(C_2) / P(host opens 3)

Computing each term:

P(C_2)                = 1/3

P(host opens 3 | C_1) = 1/2  (car at 1: host can open 2 or 3 freely)
P(host opens 3 | C_2) = 1    (car at 2: host cannot open 1 or 2, door 3 is the only option)
P(host opens 3 | C_3) = 0    (car at 3: host cannot reveal it)

P(host opens 3) = (1/2)(1/3) + (1)(1/3) + (0)(1/3) = 1/6 + 1/3 = 1/2

Substituting back:

P(C_2 | host opens 3) = (1 Γ— 1/3) / (1/2) = 2/3   β†’  switch
P(C_1 | host opens 3) = (1/2 Γ— 1/3) / (1/2) = 1/3  β†’  stay

The key term is P(host opens 3 | C_2) = 1. When the car is at door 2, Monty has no choice but to open door 3. This forced action is what shifts the probability mass. The host's lack of freedom is exactly what makes switching advantageous.

Generalization to N doors

With N doors, you pick one, the host opens Nβˆ’2 of the remaining doors (all goats), leaving exactly one alternative.

  • P(win | stay) = 1/N β€” your initial pick carries only the original prior.
  • P(win | switch) = (Nβˆ’1)/N β€” you win by switching whenever your initial pick was wrong.
N doors P(win | stay) P(win | switch) Advantage
3 33.33% 66.67% 2Γ—
4 25.00% 75.00% 3Γ—
5 20.00% 80.00% 4Γ—
10 10.00% 90.00% 9Γ—
100 1.00% 99.00% 99Γ—

The N=100 case is instructive: you pick one door out of a hundred, the host opens 98 to reveal goats, leaving yours and one other. Nobody argues 50/50 at N=100. The three-door version is the same problem β€” only the numbers are less extreme, which is exactly why the intuition fails there.

The role of host knowledge

The result depends entirely on the host knowing where the car is and always opening a goat door. If the host opened a door at random and happened to reveal a goat, the conditional probability calculation changes completely and the 50/50 intuition would actually be correct. The information asymmetry is the whole mechanism β€” Monty's constrained action communicates information about the car's location, and switching is the rational response to that information.


Quick Start

Download a pre-built binary from the releases page.

Platform File
Linux (x86_64) Monty-Hall-Problem-Simulator-Linux
Windows (x86_64) Monty-Hall-Problem-Simulator-Windows.exe
macOS (Intel) Monty-Hall-Problem-Simulator-MacOS
macOS (Apple Silicon) Monty-Hall-Problem-Simulator-MacOS-ARM64

Linux / macOS:

chmod +x Monty-Hall-Problem-Simulator-Linux
./Monty-Hall-Problem-Simulator-Linux

Windows:

Monty-Hall-Problem-Simulator-Windows.exe

Web Interface

./Monty-Hall-Problem-Simulator-Linux --web

Open http://localhost:8080 in your browser.

The web interface runs simulations server-side and streams results to the browser in real time using Server-Sent Events. No page reloads, no polling.

Live door demo β€” Three doors animate continuously. The host reveals a goat, switch and stay outcomes are shown, then the cycle repeats. Switch to Play mode to make the decisions yourself and track your personal win rate over time.

Real-time convergence chart β€” Start a simulation and watch the switch and stay win rates draw themselves toward the theoretical predictions. With the instant speed setting, a million trials complete in under a second.

N-door slider β€” Drag from 3 to 10 doors. The chart, statistics, and mathematical explanation all update immediately.

Math accordion β€” The complete Bayesian derivation and N-door comparison table, expandable at the bottom of the page.

CSV export β€” Once a simulation completes, download the full dataset for use in Python, R, or Excel.

Custom port

./Monty-Hall-Problem-Simulator-Linux --web --port 9000

Command-Line Interface

Running the binary without flags executes the full suite of simulations (10 to 1,000,000 trials) and prints results with 95% confidence intervals and delta from the theoretical value.

./Monty-Hall-Problem-Simulator-Linux
 Results after 1,000,000 simulations:
  Stay:    33.27% (Β±0.09%)  | Theory: 33.33% | Delta: 0.06%
  Switch:  66.72% (Β±0.09%)  | Theory: 66.67% | Delta: 0.05%
  Gain by switching: 33.45%

Flags

Flag Default Description
--web false Launch the web interface
--port 8080 Port for the web interface
--doors 3 Number of doors (minimum 3)
--trials 0 Custom trial count (0 = full default suite)
--graph false Show ASCII convergence graph
--output text Output format: text, json, csv
--parallel false Use all CPU cores
--verbose false Step-by-step annotated game trace
--no-math false Skip the mathematical explanation

Examples

# Step-by-step game trace followed by the ASCII convergence graph
./Monty-Hall-Problem-Simulator-Linux --verbose --graph

# Simulate with 10 doors instead of 3
./Monty-Hall-Problem-Simulator-Linux --doors 10

# Run five million trials in parallel across all CPU cores
./Monty-Hall-Problem-Simulator-Linux --trials 5000000 --parallel

# Export results to CSV for Python or Excel
./Monty-Hall-Problem-Simulator-Linux --output csv > results.csv

# Export to JSON
./Monty-Hall-Problem-Simulator-Linux --output json | python3 -m json.tool

# Simulation only, no math section
./Monty-Hall-Problem-Simulator-Linux --no-math

ASCII convergence graph

./Monty-Hall-Problem-Simulator-Linux --graph --no-math
 Convergence Graph
 Doors: 3  |  P(switch)=66.67%  |  P(stay)=33.33%
 S = Switch result  |  T = Stay result  |  --- = Theoretical

  100% |
   ...
   65% |--S-----------------------S----------S----------S-----S
   ...
   35% |--T-----------------------T----------T----------T-----T
   ...
    0% |
       +--------------------------------------------------------
        10         100        1K         10K        100K      1M

CSV output

trials,stay_pct,switch_pct,stay_ci95,switch_ci95,theo_stay,theo_switch
10,30.0000,70.0000,28.4000,28.4000,33.3333,66.6667
100,35.0000,66.0000,9.3500,9.2800,33.3333,66.6667
1000000,33.2700,66.7200,0.0920,0.0920,33.3333,66.6667

Build from Source

Requires Go 1.21+.

git clone https://github.com/franckferman/Monty-Hall-Problem-Simulator.git
cd Monty-Hall-Problem-Simulator
go build -o simulator .
./simulator

The web interface static files are embedded directly into the binary at build time using Go's embed package β€” no separate assets to ship or manage.

Cross-platform build

chmod +x build.sh
./build.sh

Produces stripped, statically linked binaries in bin/ for Linux, Windows, macOS Intel, and macOS ARM64. Release binaries are also built and published automatically via GitHub Actions on each version tag.


Contributing

Issues and pull requests are welcome. The codebase is small and deliberately self-contained β€” the simulation logic, statistics, display, and HTTP server each live in their own package under internal/.

(Back to top)


Star Evolution

Star History Chart

(Back to top)


License

GNU Affero General Public License v3.0. See LICENSE for details.

(Back to top)


Contact

ProtonMail LinkedIn Twitter

(Back to top)