Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 49 ยท Random Numbers and Statistics

Monte Carlo Simulations

Estimate pi, dice odds, the birthday problem and random walks by simulating millions of trials at once, with confidence intervals and reproducible seeds.

Some questions are hard to answer with algebra but easy to answer by trying. What is the chance that two people in a room share a birthday? How far does a drunkard wander in 400 steps? A Monte Carlo simulation answers such questions by repeating a random experiment many times and counting what happens.

NumPy turns this into a speed game. A Python for loop running a million trials takes seconds; a vectorized simulation that generates every trial as one big array takes a fraction of a second. In this lesson you will build four classic simulations, learn how to say how accurate an estimate is, and see how seeds make the results repeatable. It builds on random generators and distributions.

The vectorized pattern

Almost every simulation follows the same four steps:

flowchart LR
    A["Draw all random inputs<br/>as one array (trials, ...)"] --> B["Apply the rule<br/>with vectorized math"]
    B --> C["Collapse to one result<br/>per trial (sum, any, max)"]
    C --> D["Average over trials<br/>= estimate"]

The trial index is the first axis. Everything is computed for all trials at once, then reduced.

The key habit: never write a loop over trials. Add an axis for trials instead, and use axis=1 reductions to get one answer per trial.

Estimating pi

Throw random darts at a unit square. The fraction that lands inside the quarter circle x**2 + y**2 <= 1 is the circle's area divided by the square's area, which is pi / 4. So 4 * fraction estimates pi.

Python Estimating pi with random points

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The standard error tells you the typical size of the random wobble in the estimate. For a proportion p over n trials it is sqrt(p * (1 - p) / n). The 1.96 * se rule gives an approximate 95% confidence interval: if you repeated the whole experiment many times, about 95% of such intervals would contain the true value.

Error shrinks with the square root of the number of trials. To get one more correct digit you need about 100 times as many trials. Monte Carlo is wonderful for hard problems and wasteful for easy ones; pi has far better formulas.

Dice and probabilities

Roll two dice 500,000 times and tabulate the sums. The rolls become one array of shape (trials, 2), sum(axis=1) gives each trial's total, and np.bincount counts how often each total appears.

Python Two dice, 500,000 rolls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that rng.integers(1, 7) excludes the upper bound, just like range. The simulated frequencies match the exact fractions to about two or three decimals, and the match tightens as you add trials.

The birthday problem

How many people must be in a room before two of them probably share a birthday? Simulate it: draw a random birthday (0 to 364) for each person, sort each trial's row, and check whether any two neighbors are equal. Sorting is the trick that avoids comparing every pair.

Python The birthday problem

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With just 23 people the chance already passes 50%, which surprises most people. The exact column uses the formula 1 - (365/365)(364/365)..., and the simulation agrees within its sampling error (about 0.003 with 20,000 trials).

Random walks

A random walk takes a step of +1 or -1 at each tick. The position after t ticks is the cumulative sum of the steps, so np.cumsum(steps, axis=1) produces every path in one call. Random walks model stock prices, diffusing particles and noise in measurements.

Python Random walks and how far they wander

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The average final position is near zero, but the spread grows like the square root of the number of steps. After 400 steps the standard deviation is about 20, not 400. That square-root law is the signature of diffusion.

Memory is the limit of this style. 20,000 walks of 400 steps is 8 million int64 values, about 64 MB. For very large simulations, run in batches of trials and combine the results, as shown in Writing Fast NumPy Code.

Seeds and repeatable results

A simulation is only useful for science or testing if you can run it again and get the same answer. Seed the generator and everything downstream is deterministic. For independent streams (for example one per worker), derive child seeds with SeedSequence.spawn instead of using seed + 1, seed + 2.

Python Seeds, repeatability and independent streams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Running several independent replicates and looking at their spread is a practical way to see the uncertainty of any simulation, even when you have no formula for the standard error.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a vectorized simulation usually much faster than a Python loop over trials?

    Putting the trial index on the first axis lets NumPy run the work in compiled loops over whole arrays, avoiding Python's per-iteration overhead. Accuracy is identical.
  2. 2. You quadruple the number of Monte Carlo trials. By roughly what factor does the standard error shrink?

    The standard error scales with 1 / sqrt(n). Quadrupling n halves it.
  3. 3. What does rng.integers(1, 7, size=1000) produce?

    The upper bound is exclusive by default, like range(1, 7), so you get 1 through 6, the faces of a die.
  4. 4. What is the best way to give each of four workers its own independent random stream from one master seed?

    SeedSequence.spawn derives statistically independent child sequences from one entropy source, which is safer than hand-picked consecutive seeds.

Summary

  • A Monte Carlo simulation estimates a quantity by averaging the outcome of many random trials.
  • Put trials on the first axis, compute with array operations, then reduce with axis=1 and finally .mean().
  • Standard error shrinks like 1 / sqrt(n); for a proportion it is sqrt(p * (1 - p) / n), and estimate +/- 1.96 * se is an approximate 95% interval.
  • Sorting rows and comparing neighbors detects duplicates without comparing all pairs; np.cumsum along an axis builds random walks.
  • Seed generators for repeatability and use SeedSequence.spawn for independent streams.

Next, learn to handle gaps in real datasets in Missing Data and NaN.