Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 39 ยท Reproducibility

Seeds and Deterministic Results

Control randomness with seeds, NumPy generators and library settings so shuffles, samples, simulations and model training give the same answers on every run.

Many notebooks involve chance: shuffling rows, splitting data into training and test sets, simulating customers, initializing a model. If you run the cell twice and the numbers change, you cannot compare two experiments or check a colleague's result. The fix is to control the seed, the starting value of the random number generator.

A pseudo-random generator is a deterministic machine. Give it the same seed and it produces the same stream of numbers every time. This lesson shows how to seed correctly in Python, NumPy and pandas, what else can break determinism, and how to record the seed with the results.

Python's random module

Python
import random

random.seed(42)
print(random.random())
print(random.randint(1, 100))

Seeding sets a global generator. Everything that calls random afterwards depends on that state, including code in libraries, which is a reason to prefer local generators.

Python Same seed, same numbers

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 first two lines are identical; a different seed gives a different but equally repeatable sequence. The values are fixed by Python's generator, so they will be the same on any machine using the same Python version.

NumPy: use a Generator object

Modern NumPy recommends creating a Generator and passing it around, rather than seeding the hidden global state with np.random.seed:

Python
import numpy as np

rng = np.random.default_rng(seed=2024)
x = rng.normal(size=5)
order = rng.permutation(10)

The generator is a plain object. Functions that need randomness can accept rng as an argument, which makes them testable and independent of whatever else ran earlier in the kernel.

Python A local NumPy generator

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.

Two generators with the same seed produce the same stream. Calling rng1 again gives new numbers because the stream moved on. This is the behavior you rely on when you run cells top to bottom after a restart.

Why order matters again

A generator is a piece of state, and state is what makes notebooks fragile (see hidden state). If you seed in cell 2 and draw in cell 5 and cell 8, running cell 8 twice or skipping cell 5 changes what cell 8 sees. Two good patterns:

  • Seed once, near the top, and run top to bottom. Simple and fine for small notebooks.
  • Seed where you draw: create rng = np.random.default_rng(SEED) inside the cell or function that needs it. Every run of that cell is independent of the rest.

Keep the seed in a named constant: SEED = 20240611, so it is visible and easy to change.

Seeding common libraries

Library How to control randomness
Python random random.seed(n)
NumPy np.random.default_rng(n)
pandas df.sample(n=5, random_state=n)
scikit-learn random_state=n on splitters and models
PyTorch torch.manual_seed(n); also deterministic flags on GPU
TensorFlow tf.random.set_seed(n)

pandas and scikit-learn take the seed as an argument, which is the cleanest style: there is nothing global to forget. A call such as train_test_split(X, y, test_size=0.2, random_state=0) is repeatable.

Python A repeatable shuffle and split

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.

Reproducing the same train and test sets is essential when comparing models: if the split changes, a difference in score might be luck rather than a better model.

What else breaks determinism

Seeds are necessary but not always sufficient.

  • Parallel code. Threads or processes that draw random numbers in a different order give different results. Give each worker its own seeded generator (NumPy's SeedSequence.spawn is made for this).
  • Hash randomization. Python randomizes hash() for strings between runs, so iteration order of a set of strings can change. Sort before using, or set PYTHONHASHSEED.
  • Floating point. Summing floats in a different order (for example across GPU threads) gives tiny differences.
  • Time and the outside world. datetime.now(), network data and files that change are not random, but they are not repeatable either. Pin them: use a fixed "as of" date and keep a copy of the input data.
  • Library versions. A new release may change how a seed maps to numbers. Another reason to pin dependencies.

Record what you used

Print the seed and the versions with the results, or store them in the output file. A line such as seed=20240611, numpy=2.1.3, python=3.12.4 in the notebook header makes a result traceable. For experiments you intend to compare, run several seeds and report the spread (mean and standard deviation) rather than one lucky number.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a random seed do?

    Pseudo-random generators are deterministic: the same seed gives the same stream.
  2. 2. Which NumPy style is recommended today?

    A local Generator avoids hidden global state and works well in tests.
  3. 3. How do you make df.sample(5) repeatable?

    pandas takes the seed through the random_state argument.
  4. 4. Which is a source of non-determinism that a seed does NOT fix?

    Changing inputs, wall-clock time and unordered parallel work produce differences regardless of seeds.

Summary

  • Seed the generator and keep the seed in a visible constant.
  • Prefer local objects (default_rng, random_state=) over global seeding.
  • Parallel workers, hash order, floating-point order, changing inputs and library upgrades can still change results.
  • Record the seed and versions with every result you may need to reproduce.

Next: The JupyterLab Interface