A Monte Carlo simulator for the Monty Hall Problem
Project description
MontyCarlo
A Monte Carlo simulator for the Monty Hall Problem
A.K.A.
"Two goats, one car"
This project simulates the classic Monty Hall problem and estimates the empirical win rates for staying vs switching. It adheres to the standard rules, and prints clean JSON for easy piping to other tools.
Rules
- The prize is hidden uniformly at random behind one of three doors.
- The contestant picks one door uniformly at random.
- The host opens one of the other two doors, and reveals a goat. The host always knows where the prize is, and never opens the contestant’s chosen door or the prize door.
- The contestant may stay with the original door or switch to the remaining closed door.
The MontyCarlo simulator runs a large number of independent trials conforming to these rules and reports the observed win rates for both strategies.
Requirements
- Python >=3.10
Installation
git clone https://github.com/DJStompZone/MontyCarlo.git
cd MontyCarlo
pip install .
Or install directly with pip:
pip install two-goats-one-car
Project Layout
MontyCarlo/
├── LICENSE
├── pyproject.toml
├── README.md
└── two_goats_one_car/
├── __init__.py
├── __main__.py # CLI entrypoint (python -m montycarlo)
├── monty.py # Monty class & simulation logic
└── results.py # Dataclasses for trial results
Usage
Arguments
-n, --trials— Number of trials to run (default:100000).-s, --seed— PRNG seed for reproducibility (default: none).
Reproducibility
- Set
--seedto fix the RNG stream. - Larger
-nreduces sampling noise - Expect absolute error to shrink roughly with $(O(1/\sqrt{n}))$.
Example
# Run 1,000,000 trials with a fixed seed:
montycarlo -n 1000000 -s 69420
Example Output
{
// Number of trials run
"trials": 1000000,
// Empirical probability of winning by staying
"stay_win_rate": 0.333021,
// Empirical probability of winning by switching
"switch_win_rate": 0.666979,
// Difference between switch and stay win rates
"delta": 0.333958
}
License
MIT. See the LICENSE file for details.
Disclaimer
Don't come crying to me if you try this on a real game show and still pick the goat. :D
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file two_goats_one_car-1.0.2.tar.gz.
File metadata
- Download URL: two_goats_one_car-1.0.2.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c51ea05d0a6f1b51fd6831495135a65e9216f250eb232cc11a03e3cd2ec1f7d
|
|
| MD5 |
42c4bb762eeec49144eba70dca57c29c
|
|
| BLAKE2b-256 |
2efc384f3c93e1734e78ace07167ec6b62bf77081314a85eaeb19156afedc6eb
|
File details
Details for the file two_goats_one_car-1.0.2-py3-none-any.whl.
File metadata
- Download URL: two_goats_one_car-1.0.2-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.11 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4fa3da0dc99bc80c183f3d5ce8d78140959c663e75c7dc70624ff7f56f71028
|
|
| MD5 |
2b89b0bfd466a9e889d93f41cd405460
|
|
| BLAKE2b-256 |
6393b2932ce5554d992bfa2e970c4fe1411bd9c89291b499e91b001317688ae1
|