Every project / Techy

🎲 Probability Lab

Roll dice 10,000 times, estimate π by throwing random darts, and see math turn into pictures.

Probability Lab running
Language
Python
Level
Intermediate
Category
Techy
▶ Try it & remix

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).

SettingStarts atFileWhat it does
ROLLS10_000main.pyhow many rolls and darts (more = more accurate, but slower)

Files

FileLines
main.py34where the program starts

Challenges

Each one is a bit harder than the last. Teachers: these make good assignment instructions.

  1. Roll three dice instead of two. What's the most common total now?
  2. Make ROLLS bigger. Does the estimate for π get closer?
  3. Simulate the birthday problem: how many people until two share a birthday?
  4. Simulate flipping a coin until you get 5 heads in a row. How long does it take on average?
  5. Draw the histogram sideways (vertical bars) instead.