Replay One Random Sequence
Recreate one generated sequence from its algorithm, seed, and ordered calls while keeping generator ownership visible.
A seed is useful only as part of a complete replay contract. To reproduce one generated sequence, keep the generator algorithm, seed, and ordered calls the same.
Replay the Same Starting State and Calls
We continue with the calibrated table:
Two generators created from the same seed begin in corresponding states:
same noise: True
same result: True
The match is exact because both branches use the same NumPy generator
algorithm, seed 17, first operation uniform, interval, shape, and call
position. The seed establishes a starting state; it does not override the
state changes caused by later calls.
NumPy's default_rng selects its current default generator algorithm. A saved
seed alone is not a promise that every future NumPy version or different
software environment will produce the same sequence. Within the reviewed
environment, record the algorithm choice, seed, and calls that define the run.
Q1. Identify a replayable pair of runs
Which two runs are expected to agree under the same reviewed NumPy environment?
Select one choice, then check.
HintCompare the complete sequence
Check the seed, then compare every generator call before and including the value being reproduced.
SolutionMatch seed and ordered calls
The first pair is replayable: both generators start from seed 17 and receive
identical requests in the same order. An extra call advances one state, and
a different seed changes the starting state.
Pass One Generator Through the Run
A function that needs variation should receive the generator it uses. That makes state ownership visible:
The function does not choose a hidden seed or create a private generator. A run creates one generator at its boundary and passes it into each operation:
first agrees: True
second agrees: True
calls differ within run: True
The first call in run A matches the first call in run B. The second matches the second. Within either run, the two results differ because the first request advances that run's generator before the second request.
Passing rng also makes tests and callers responsible for the sequence. A
reader can see that changing the number or order of calls changes later values.
There is no hidden module-level generator to inspect or reset.
Q2. Replay two complete noisy runs
Complete the two functions so the run owns one generator and passes it into two
ordered noise calls. Two runs with seed 17 should agree call by call.
Editable Python
Ready to run.
HintKeep generator creation at the run boundary
In complete_run, create rng = np.random.default_rng(seed). Pass that same
object to add_bounded_noise twice. Inside the helper, request noise from the
received rng and add it to calibrated.
SolutionPass one advancing generator through each run
Corresponding calls across the two runs agree. Successive calls within one run use different generator states.
Keep Call Order Part of the Contract
An extra request changes every later state position:
Both generators began with seed 17, but their uniform calls occur at
different positions in their state sequences. Recording only the seed would
hide the reason for the mismatch.
Creating a new seeded generator inside repeated work causes the opposite problem:
Every call restarts the same sequence, so repeated work receives the same first noise table. That may look reproducible, but it does not represent successive draws from one run. Create the generator once at the run boundary and pass it to repeated operations. Recreate it from the recorded contract only when the purpose is to replay the complete run from its beginning.
Q3. Repair accidental reseeding
A loop calls a helper five times. The helper creates default_rng(17) on every
call, so all five noise arrays are identical. Which repair preserves a
replayable sequence of five successive requests?
Select one choice, then check.
HintMove ownership outside repeated work
The loop belongs to one run. Let that run create and own one generator before the first iteration.
SolutionPass one generator through the loop
Create rng = np.random.default_rng(17) once before the loop. Give the same
rng to each helper call. Replaying the loop with the same algorithm, seed,
and call order reproduces all five outputs in order.
A replay contract includes the generator algorithm, seed, and every ordered request that advances state. Create one generator at the run boundary and pass it into functions that need variation. Restart from the seed only to replay the complete call sequence, not inside repeated work. The next lesson uses that generator to select identified records under an explicit sampling rule.