Random: better handling of the "global seed" (using TLS) #51526
Merged
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.
We maintain a "global seed" for this feature of
@testset
:But since we don't use
MersenneTwister
as the "global RNG" anymore, we need to maintain a separate "global seed" object. So far we literally used a global objectRandom.GLOBAL_SEED
storing the original seed, but it's not robust when multi-tasking is involved: e.g.So we store here a "global seed" in
task_local_storage()
, which is set whenseed!()
is invoked without an explicit RNG, and defaults toRandom.GLOBAL_SEED
, which is set only once whenRandom
is loaded. And instead of actually storing a seed, we store a copy of the RNG state.This is still not ideal, in that at the beginning of
@testset "A"
or@testset "B"
, we can't do@test x == rand()
, because these are in separate tasks, so the global seed defaults toRandom.GLOBAL_SEED
, and not to the global seed of the parent's task; there might be a nice way to handle that, but at least different tasks don't corrupt each-other's seeds.