MersenneTwister: simplify seeding logic #60204
Open
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.
Since at least v0.3,
MersenneTwisterhas stored the user-provided seed. More recently, this allowedshowto print a round-trippable representation of the RNG. However, storing an arbitrary deep-copied user seed is not ideal, and the initialization logic has become more complicated than necessary.In particular,
MersenneTwister()currently performs three steps:seed = rand(RandomDevice(), UInt128)# generate a seedinitstate = rand(SeedHasher(seed), UInt32, 8)# expand itinitstateThis commit merges the first two steps. The new initialization is:
initstate = rand(RandomDevice(), NTuple{2, UInt128})initstateto the dSFMT format and initialize the stateWe now store only
initstatefor use inshow, preserving round-trippability. For user-provided seeds,initstateis derived from the seed viaSeedHasher.A small bonus is that
MersenneTwister()is no longer restricted to "only" 2^128 distinct initializations.The cosmetic drawback is that the printed representation is less pretty:
MersenneTwister(1)now shows asThis is based off #60185 to avoid merge conflicts.