Validate and communicateLesson 58 of 76 · 8 min

Give parallel runs independent streams

Create replayable child random streams without duplicating work.

A seed controls a generator’s initial state. Starting every worker from the same seed repeats the same sequence and does not provide independent simulation batches. Parallel execution needs a deliberate stream design.

NumPy SeedSequence can spawn reproducible child sequences intended for separate generators. Keep each child’s role and the parent seed recorded. Stream design is distinct from operating-system scheduling: this lesson creates the streams sequentially so the numerical behaviour remains inspectable.

When using chunks, avoid resetting a generator to the same state for every chunk. That would repeatedly recycle the same random inputs. Record enough metadata to reproduce the procedure, including package versions, algorithm, draw order and any changes in how workers receive streams.

Spawn streams, not duplicate seeds
  1. Record a parent seed
    SeedSequence⁡(2026)\operatorname{SeedSequence}(2026)
  2. Spawn child streams
    G1,G2,G3,G4G_1,G_2,G_3,G_4
  3. Replay the same spawn tree

    same child sequences

Reference illustration. The source uses SeedSequence.spawn to create separate reproducible child streams. Starting every worker with the same seed would duplicate draws rather than add independent numerical evidence.

What happens if every worker is initialised with exactly the same seed?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Child-stream first draws: [[629042, 382691, 176495, 20440], [748907, 671112, 646693, 157999], [88653, 550844, 899179, 233926], [465928, 743334, 802150, 178884]]
Replay identical: True
Spawn keys: [(0,), (1,), (2,), (3,)]

Read the first result

The child streams differ while the overall construction can be replayed. Different sample values alone would not prove arbitrary generators independent.

Try an edit

Add print("Same-seed batches:", [np.random.default_rng(2026).integers(0, 1000000, 4).tolist() for _ in range(4)]) immediately after print("Spawn keys:", [child.spawn_key for child in children]). Select Run Python again. Compare Same-seed batches with Child-stream first draws. How many distinct sequences are in each list?

Back to the Python editor

Scope. The example illustrates stream construction without running actual parallel workers or claiming a parallel speedup.

Try a fresh case

Which record best supports replay of a multi-stream simulation?

Choose an answer
Need a hint?
Hint 1

List what determines each worker’s random inputs.

Hint 2

Keep the assignment rule as well as the seeds.

Your notes