This project is a C++ command-line lottery simulation program that models real-world lotto systems using probability, combinatorics, and randomized dice rolls.
It simulates lottery drawings, calculates true odds mathematically, and repeatedly “plays” until a winning combination is achieved — tracking total rolls, cost, and statistical averages.
This project is a console-based C++ lottery simulator designed to demonstrate:
- Combinatorics-based odds calculation (n choose k)
- Random number generation using
std::mt19937_64 - Simulation of lottery roll repetition until win
- Duplicate prevention logic
- Sorting algorithms (insertion sort)
- Performance throttling based on CPU workload selection
- Statistical averaging across multiple lottery simulations
- File parsing using
fstreamandstringstream
The program allows users to:
- Choose predefined lottery systems (via
lottoInfo.txt) - Create a fully custom lottery configuration
- Simulate one or many lottery runs
- Calculate average rolls across multiple simulations
- Track total cost vs jackpot winnings
Write about how to use this project.
- C++17 compatible compiler
- g++ (Mac/Linux) or MinGW-w64 (Windows)
- VS Code (optional but recommended)
From the project directory:
g++ -std=c++17 main.cpp dice.cpp -o lottoRun:
./lottoIf dice.cpp is not separate and is header-only, compile:
g++ -std=c++17 main.cpp -o lotto- Install MinGW-w64
- Ensure
g++is in your system PATH - Open VS Code in the project folder
- Open terminal inside VS Code
- Compile:
g++ -std=c++17 main.cpp dice.cpp -o lotto.exeRun:
lotto.exeWhen launched:
- Choose CPU workload mode
- Select an existing lottery or create custom
- Program simulates rolls until jackpot win
- Displays:
- Number of rolls required
- Closest roll sets
- Total ticket cost
- Time elapsed
- Average across repeated runs (if selected)
You may enter:
y,yes, etc. to replay- A number to simulate multiple runs for averaging
If you want to modify or extend this program, follow the structure below.
- C++17 knowledge
- Understanding of:
- Random number generation
- Combinations (nCk)
- Sorting algorithms
- File I/O
- Vectors and dynamic arrays
Lotterystruct for configuration storagecalculateOdds()for mathematical oddsplayLottery()for simulation enginedoubles()legacy simulation mode- Insertion sort for roll normalization
- Thread-local Mersenne Twister RNG
- CPU workload throttling using
sleep_for - Rolling statistics & averaging system
Only the primary structure is shown below.
.
├── main.cpp
├── dice.h
├── dice.cpp
└── lottoInfo.txt
| No | File | Details |
|---|---|---|
| 1 | main.cpp | Core lottery logic and simulation engine |
| 2 | dice.h | Dice class declaration |
| 3 | dice.cpp | Dice class implementation |
| 4 | lottoInfo.txt | Predefined lottery configurations |
- Ensure
lottoInfo.txtis in the same directory as the executable - Must compile with
-std=c++17or newer - Program uses
<thread>and<chrono>for timing
Original Author from the 1980's (doubles function): Alen Bell
Updated & Documented by: Kenton Bell
Built using:
- C++17
- Standard Template Library (STL)
- Mersenne Twister RNG (
std::mt19937_64) - VS Code