Skip to content

Write a function that makes randomize() reproducible

write-from-scratch / best-practice

Two separate calls on a handle:

handle.randomize()    -- rolls new values into the rand fields from the object's current PRNG state.
handle.srandom(seed)  -- randomizes nothing. It resets the object's PRNG from `seed`, so every later randomize() follows the same sequence for that seed.

srandom() isn’t an argument to randomize(); call it on the handle first.

Fix the function below.

RequirementsFor make_seeded_item()

  1. It returns a valid (non-null) handle.
  2. Two calls with the same seed give objects whose later randomize() calls produce the same sequence.
  3. Two calls with different seeds give sequences that diverge.

Source on GitHub

missing_seed_reproducibility.sv