fix(interpreter): seed $RANDOM PRNG per-instance#1222
Merged
Conversation
…quences Replace the per-access RandomState hash approach with a properly seeded LCG (linear congruential generator) stored per-interpreter instance. The PRNG is seeded from OS entropy at construction time and uses the glibc LCG constants (state * 1103515245 + 12345), matching real bash behavior. RANDOM=N reseeds the PRNG for deterministic replay. Uses AtomicU32 for interior mutability so $RANDOM can advance state in expand_variable(&self) while keeping the Interpreter Send+Sync. Closes #1180
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
Closes #1180
RandomState::new()with a stored LCG PRNG state (AtomicU32) seeded from OS entropy at interpreter creationstate * 1103515245 + 12345) matching bash behaviorRANDOM=Nreseeding (assigning to RANDOM sets the PRNG seed, consistent with real bash)Test plan
test_random_different_instances— two Bash instances produce different first valuestest_random_reseed—RANDOM=42on two instances yields identical sequencestest_random_sequential_varies— three sequential calls produce varying values