Fix serial-path reproducibility for L'Ecuyer-CMRG (list) seeds#89
Open
mronkko wants to merge 1 commit into
Open
Fix serial-path reproducibility for L'Ecuyer-CMRG (list) seeds#89mronkko wants to merge 1 commit into
mronkko wants to merge 1 commit into
Conversation
set_seed() assigned `.Random.seed` to its local function frame rather than .GlobalEnv when given a list-type (L'Ecuyer-CMRG) seed. R's RNG only consults globalenv()$.Random.seed, so the seed state was silently discarded on the serial (non-parallel) path. This made runArraySimulation(..., iseed, parallel = FALSE) and runSimulation(seed = <genSeeds() list>, parallel = FALSE) non-reproducible across runs, while the parallel path (which installs the seed via clusterSetRNGSubStream() into the workers' global env) was unaffected. Assigning into .GlobalEnv installs a valid L'Ecuyer-CMRG state; R derives the RNG kind from .Random.seed[1], so the generator switches automatically. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
set_seed()did not actually apply list-type (L'Ecuyer-CMRG) seeds on the serial (non-parallel) execution path, so seeded runs were not reproducible whenparallel = FALSE.The helper assigned the seed to its local function frame:
R's RNG only ever consults
globalenv()$.Random.seed, so this assignment had no effect and the L'Ecuyer-CMRG state was silently dropped. The serial loop then ran off the ambient global RNG state.This affected:
runArraySimulation(..., iseed = <int>, parallel = FALSE)—genSeeds()produces a length-1 list holding a 7-integer L'Ecuyer state, which was dropped.runSimulation(seed = genSeeds(design, iseed = ...), parallel = FALSE)— same list-seed path.The parallel path was unaffected, because it installs the seed via
clusterSetRNGSubStream()/clusterSetRNGStream(), whichassign()into the workers'.GlobalEnv.Fix
One-line change: assign into
.GlobalEnv(matching the idiom already used atanalysis.R:21andutil.R:711):Setting a valid L'Ecuyer-CMRG vector as the global
.Random.seedalso switches the active generator automatically, since R derives the RNG kind from.Random.seed[1]. No explicitRNGkind()call is needed (the array path already sets it).Verification
Before the fix, two serial runs with the same
iseedproduced different per-replication draws; after the fix they are byte-identical (and parallel runs remain reproducible as before):parallel = FALSEparallel = TRUEA regression test asserting serial-path reproducibility of
runArraySimulation(..., iseed, parallel = FALSE)is added totests/tests/test-03-array.R.🤖 Generated with Claude Code