Random numbers and simulation

Intermediate

Introduction

Random numbers drive simulations, games, statistical tests, train/test splits and the starting weights of neural networks. Computers produce them with pseudo-random generators: deterministic algorithms whose output only looks random. That determinism is a feature: with the same seed, a simulation gives the same result every time, so others can check it.

How it works

Generators. Modern NumPy code creates one generator and draws everything from it:

rng = np.random.default_rng(42)
rng.integers(1, 7, size=5)        # five dice rolls: high is excluded, like range
rng.random(3)                     # three floats in [0, 1)
rng.normal(170, 10, size=1000)    # 1000 heights, mean 170, sd 10
rng.choice(["a", "b", "c"], size=2, replace=False)

The same seed always gives the same numbers, on any computer with the same NumPy version. The older np.random.seed() and np.random.rand() style still works but is not recommended for new code. NumPy's generator and Python's random module produce different sequences, even with the same seed; this lesson's runnable examples use Python's random to show the same ideas.

How a generator works. The simplest kind is a linear congruential generator: x(n+1) = (a·x(n) + c) mod m. With a = 5, c = 3, m = 16 and seed 1, it gives 8, 11, 10, 5, 12, 15 and so on, and x ÷ m is a number between 0 and 1. NumPy uses a much better algorithm (PCG64), but the principle is the same: a hidden state updated by a fixed rule.

Shuffling. rng.shuffle(a) shuffles in place; rng.permutation(a) returns a shuffled copy. Both use the idea of the Fisher–Yates shuffle: for each position i from the last down to 1, pick a random j between 0 and i and swap a[i] with a[j]. Every order is equally likely.

Simulation. The Monte Carlo method estimates a quantity by averaging random trials. To estimate π, throw random points into the unit square: the share that lands inside the quarter circle x² + y² < 1 is about π ÷ 4. With NumPy it is one line, 4 * np.mean(x**2 + y**2 < 1). The estimate has a standard error: for a share p from n trials, √(p(1 − p) ÷ n). Ten times more accuracy needs a hundred times more trials.

Example

# Random numbers with a seed, shown with Python's random module.
# NumPy's rng = np.random.default_rng(42) works the same way, but produces different numbers.
import random

a = random.Random(42)
b = random.Random(42)
print("generator a:", [a.randint(1, 6) for _ in range(5)])
print("generator b:", [b.randint(1, 6) for _ in range(5)])

rng = random.Random(7)
rolls = [rng.randint(1, 6) for _ in range(60)]
print("counts of faces 1-6:", [rolls.count(face) for face in range(1, 7)])
print("mean roll:", round(sum(rolls) / len(rolls), 2))
Open in compiler

Line by line

  1. Two generators with the same seed, 42, produce exactly the same five rolls.
  2. A third generator, seed 7, rolls a die 60 times.
  3. The counts of each face vary around the expected 10, and the mean is near 3.5.

Expected result

generator a: [6, 1, 1, 6, 3]
generator b: [6, 1, 1, 6, 3]
counts of faces 1-6: [18, 9, 5, 6, 16, 6]
mean roll: 3.18

In the real world

Scientists publish seeds with their simulations so results can be repeated. Machine learning frameworks seed their generators to make training runs reproducible. Banks estimate risk by simulating thousands of possible market paths, and games use seeded generators so that a level can be regenerated from a single number.

Common mistakes

  • Expecting rng.integers(1, 6) to include 6: the upper bound is excluded.
  • Creating a new generator with the same seed inside a loop, which repeats the same numbers.
  • Not setting a seed in work that others must reproduce.
  • Trusting a simulation with too few trials, without estimating its error.

Best practices

  • Create one generator at the top with a documented seed and pass it to the functions that need it.
  • Use rng.permutation for a reproducible random train/test split.
  • Report a simulation's estimate with its standard error.

Summary

np.random.default_rng(seed) creates a pseudo-random generator; the seed makes results reproducible. Draw integers, floats, normal values and choices from it, shuffle with shuffle or permutation (Fisher–Yates), and estimate quantities by Monte Carlo simulation, whose error shrinks with √n.

Quick check

1. What can rng.integers(1, 7) return?
2. Why set a seed?
3. To make a Monte Carlo estimate 10 times more precise you need about…
4. Which call returns a shuffled copy and leaves the original unchanged?

Key takeaways

  • Create one generator with default_rng(seed) and draw everything from it.
  • rng.integers(low, high) excludes high, like range.
  • A simulation's error shrinks with the square root of the number of trials.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.