Codectionary / Developer documentation / Python

Reproducible randomness

Use independent pseudo-random generators for repeatable demonstrations and tests.

How it works

random provides pseudo-random generators for simulations and sampling. A seed initialises their state; it does not make each call return the same value. Successive calls advance the state.

A dedicated Random instance isolates a sequence from unrelated calls to the module-level generator. For reproducible experiments, record the seed, algorithm, Python version and order of calls. Higher-level sampling algorithms may change across Python versions.

Limitations and practical choices

Do not use random for passwords, reset tokens or other security secrets. Use secrets for those purposes.

Do not reseed inside every iteration unless restarting the sequence is genuinely intended. A repeatable simulation is useful, but it is not evidence that its statistical assumptions are correct.

Try it yourself

Create two generators with the same seed. Compare their first three random values, then consume an extra value from one and explain why they no longer align.

Syntax

from random import Random
rng = Random(42)
value = rng.random()

Examples

Repeat the same experiment

Show that equal initial states produce matching sequences.

from random import Random
left = Random(7)
right = Random(7)
left_values = [left.random() for _ in range(3)]
right_values = [right.random() for _ in range(3)]
print(left_values == right_values)
  1. Each generator begins with the same seed and receives the same three calls. Their states advance in step.
  2. The comparison prints a stable Boolean instead of depending on a rounded display of particular random values.
  3. Use a recorded seed to reproduce a simulation case while keeping generators separate from unrelated application code.
  4. Expected output: True

Restore a saved state

Replay the next draw without restarting from the initial seed.

from random import Random
rng = Random(12)
rng.random()
checkpoint = rng.getstate()
first = rng.random()
rng.setstate(checkpoint)
replayed = rng.random()
print(first == replayed)
  1. The first unused draw advances the generator. getstate then captures its current position, not merely its original seed.
  2. After drawing once, setstate restores the checkpoint. The next draw matches the saved first result.
  3. A checkpoint helps replay a local debugging scenario; do not assume state objects are a permanent interchange format across implementations.
  4. Expected output: True

Sample without replacement

Choose distinct positions from a population.

from random import Random
rng = Random(5)
population = ["red", "blue", "green", "yellow"]
chosen = rng.sample(population, k=3)
print(len(chosen))
print(len(set(chosen)) == 3)
print(population)
  1. sample selects three positions without replacement and returns a new list. The original population stays unchanged.
  2. The distinct-value check is true because the input values are unique; duplicated values in the population could still appear in a sample.
  3. Use sampling for a reproducible practice selection, not a security-sensitive lottery or token generator.
  4. Expected output: 3 True ['red', 'blue', 'green', 'yellow']

Best practices

  • Do not use random for passwords, reset tokens or other security secrets. Use secrets for those purposes.
  • Do not reseed inside every iteration unless restarting the sequence is genuinely intended. A repeatable simulation is useful, but it is not evidence that its statistical assumptions are correct.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading