An over-engineered CLI app, and an experiment with TypeScript/JavaScript (ECMAScript) aimed at:
- Creating a simulation of the famous counterintuitive Monty Hall problem
- Solving the strategy problem with Monte Carlo methods
- Creating a simulation of a general Montehall Problem with
$n$ number of doors - Producing fairly readable code and Node.js solution suitable for a pretty good practice tutorial.
Inspired by Numberphile's Monty Hall problem video.
Node.js (latest v16.x, or higher) and NPM (v9.x) are required to run the app:
npm install
npm run build
Or, using Docker and Bash (recommended)
docker pull node
docker run --rm --volume=$(pwd):/src --workdir="/src" node npm install
docker run --rm --volume=$(pwd):/src --workdir="/src" node npm run build
Running the default simulation with a player that does not switch:
npx montehall
or, with Docker and Bash
docker run --rm --volume="$(pwd):/src" --workdir="/src" node npx montehall
A player who does not switches their door pick should statistically win 1/3
of the games, given a sufficiently large number of random game attempts.
However, a prudent, or "wise," player who switches pick should win 2/3
of the games, which can be simulated using the --wise
option:
npx montehall --wise
A list of available options is accessible from the CLI:
npx montehall --help
The default number of simulated games is 16384.
This can be changed using the --games
option, or by changing the value for
the games
key in the config.json
configuration file.
A standard (default) game has three doors. A general game has
The CLI option --doors <number>
can be used to specify the number of doors.
Examples:
npx montehall --doors 100
npx montehall --doors 181 --games 1000 --wise
A randomness source is required to simulate the games. Achieving high-quality simulation results requires a high-quality randomness source.
The default source of randomness used is a fast pseudo one: the
Math.random()
function.
For a higher quality, but slower, randomness source,
use the --random advanced
option, which employs a cryptographically
secure pseudorandom number generator (CSPRNG.)
npx montehall --wise --random advanced
A text file of numbers (random positive integers) can be loaded to run a deterministic simulation.
The file path can be passed as a CLI option:
npx montehall --games 100 --table-file vendor/numbers.txt
Or, alternatively, the file path can be configured in config.json
.
Example:
{
"numbersFilePath": "./data/numbers.txt"
}
npx montehall --games 100 --random table
The file must have one number per text line. Example:
1
1
0
1
2
1
0
See vendor/numbers.txt
for a sample file.
Partial list of showcases, in alphabetical order:
- Asynchronous programming
- Azure Pipelines
- Behavior-driven development (BDD)
- CodeQL
- Coding without
null
- Coding without classes
- Command-line interface (CLI)
- Commander.js
- Continuous integration
- Conventional Commits
- DevSkim
- Distribution package clean of development dependencies
- ECMAScript Modules
- ESLint
- Functional programming
- GitHub Actions
- High code test coverage
- Markdown
- Markdownlint
- Node.js
- NPM
- Prettier
- SonarCloud
- Strict static rules and type safety
- Symantec versioning
- TypeScript
- Unbiased pseudo-randomness
- Unit testing with Jest (and Jasmine)
This software is released under an MIT-style license. Copyright © 2017-2023 Omar Boukli-Hacene.
Written for the joy of it 🐻