Every project / Techy
🎲 Probability Lab
Roll dice 10,000 times, estimate π by throwing random darts, and see math turn into pictures.
- Language
- Python
- Level
- Intermediate
- Category
- Techy
What you'll learn
- Simulations
- The random module
- Counter & dictionaries
- Text bar charts
- Monte Carlo methods
How it works
- Counter tallies 10,000 rolls of two dice.
- The bars are scaled so the most common total is 30 blocks wide.
- The math check: a total's chance is how many dice pairs add up to it, out of 36.
- The π estimate throws random darts at a square. The share landing inside the quarter circle is π/4.
Knobs to turn
These settings are near the top of the code. Remix it, change one, and watch what happens. The preview updates as you type (press ▶ Run for Python).
| Setting | Starts at | File | What it does |
|---|---|---|---|
ROLLS | 10_000 | main.py | how many rolls and darts (more = more accurate, but slower) |
Files
| File | Lines | |
|---|---|---|
main.py | 34 | where the program starts |
Challenges
Each one is a bit harder than the last. Teachers: these make good assignment instructions.
- Roll three dice instead of two. What's the most common total now?
- Make ROLLS bigger. Does the estimate for π get closer?
- Simulate the birthday problem: how many people until two share a birthday?
- Simulate flipping a coin until you get 5 heads in a row. How long does it take on average?
- Draw the histogram sideways (vertical bars) instead.