Seeds and Repeatable Sequences

A seed chooses the starting state of a pseudorandom sequence. Create separate generators from recorded seeds, verify what an equal seed repeats, and distinguish restarting a sequence from continuing to draw values from one generator.

A pseudorandom generator produces a sequence by repeatedly updating an internal state. A seed supplies the information used to create that starting state. When the generator algorithm, seed, and sequence of method calls match, the generated values can be reproduced.

Here, 42 is the seed. It is not a generated value and does not force every draw to equal 42. It identifies a repeatable starting point for the generator.

Recreate a sequence from the same seed

Two newly created generators with the same seed begin in the same state:

Because both generators receive the same method call with the same arguments, first and second contain equal values.

Recreate one integer sequence

Each generator starts from seed 42 and receives the same draw. Change only one seed and compare the arrays again.

Ready to run.

The important statement is not “seed 42 produces meaningful numbers.” Another integer seed would also work. The seed matters because it is recorded and reused.

One generator advances after every draw

A generator does not return to its starting state after a method call. Drawing values changes its internal state:

second_draw is the next part of the sequence, so it normally differs from first_draw.

To reproduce both draws, recreate the generator from the seed and repeat the same calls in the same order:

Now replayed_first matches first_draw, and replayed_second matches second_draw.

The sequence of calls is part of the replay

A seed establishes the starting state, but it does not describe how the program consumes the sequence. An additional draw changes every later value:

left and right need not match. The second generator consumed one value before producing right.

Changing the method, requested shape, or call order can also advance the state differently. Reproducibility therefore requires the seed and the code path that uses the generator.

The following example makes this visible without depending on any particular generated numbers.

Compare continued and restarted sequences

The continued generator advances, while the restarted generator returns to the initial state. Edit the draw sizes and observe which comparisons remain true.

Ready to run.

The first two comparisons are true because the entire call sequence is replayed. The last comparison is normally false because consecutive draws use different generator states.

Avoid restarting inside repeated work

Suppose a loop needs five simulated rolls. Recreating the same generator inside the loop restarts the sequence five times:

Each iteration performs the same first draw, so all five values match. That is usually not the intended simulation.

Create one generator before the loop and let it advance:

An array draw is shorter when all rolls have the same role:

Repeated values can still occur naturally. The difference is that the generator was allowed to advance rather than being forced back to the same starting state.

Pass a generator into randomized functions

Hiding generator creation inside a function makes control harder:

The caller cannot choose or record the generator that produced the noise. A clearer function accepts a generator:

The caller now controls one sequence:

This pattern also makes tests easier. A test can pass a generator with a known seed and reproduce the same sequence of draws.

Know what a seed does not guarantee

A seed controls the pseudorandom sequence produced by one generator. It does not by itself guarantee that an entire experiment will reproduce exactly. Results can still depend on:

  • different input data or preprocessing;
  • changed code or configuration;
  • a different sequence of random calls;
  • package or generator-algorithm changes;
  • numerical operations that vary across hardware or execution environments;
  • other libraries or devices with their own random generators.

For this reason, record the seed with the configuration and result rather than treating it as a complete experiment record. Later lessons build that larger record.

Seeds in this chapter support repeatable numerical work. They do not make pseudorandom values suitable for passwords, access tokens, or cryptographic keys.

Exercise: State the purpose of a seed

What does a seed provide in the examples in this chapter?

Choose the purpose

Select one choice, then check.

HintThink about replay

Reusing the seed with the same generator algorithm and calls allows a sequence to begin again.

SolutionA seed establishes a repeatable starting state

The seed initializes the generator. It does not make values constant or make the generator suitable for security-sensitive values.

Exercise: Predict two consecutive draws

Consider this program:

Which statement is correct?

Choose the generator behavior

Select one choice, then check.

HintFollow one generator object

No new generator is created before second.

SolutionThe second draw continues the sequence

first advances rng. The call that creates second begins from the new state rather than restarting from the seed.

Exercise: Choose a complete replay

Which program reproduces both arrays created by these calls?

Choose the replay

Select one choice, then check.

HintRepeat every state-changing call

The first draw changes which values the second draw receives.

SolutionUse the same seed and call order

A generator created from seed 11, followed by random(3) and then random(2), replays both arrays in their original positions.

Exercise: Repair repeated reseeding

The following loop produces the same first draw on every iteration:

Which change preserves repeatability while allowing the generator to advance?

Choose the repair

Select one choice, then check.

HintMove initialization, not generation

The repeated work should draw values, not recreate the starting state.

SolutionCreate the generator before the loop

Initialize rng = np.random.default_rng(8) once before the loop. Every iteration then consumes the next value from one repeatable sequence.

Exercise: Verify a restarted sequence

Complete the program so replay_rng begins from the same seed as original_rng and repeats both draws. The output must include:

first matches: True
second matches: True

Replay two ordered draws

Ready to run.

HintRepeat initialization and consumption

Use the recorded seed, then repeat integers(0, 50, size=3) before integers(0, 50, size=5).

SolutionReplay the complete call sequence

The new generator starts from seed 23. Repeating both draws in their original order advances it through the same states and reproduces both arrays.

Retain the seed–state–calls model

Use this model when a repeated result differs:

seed
-> initial generator state
-> ordered random calls
-> advancing states
-> generated values

Check the seed first, then check whether the same generator algorithm and the same calls occurred in the same order. A matching seed cannot repair a changed random call sequence.