Skip to content

fix(interpreter): seed $RANDOM PRNG per-instance#1222

Merged
chaliy merged 2 commits intomainfrom
fix/1180-random-prng-seeding
Apr 11, 2026
Merged

fix(interpreter): seed $RANDOM PRNG per-instance#1222
chaliy merged 2 commits intomainfrom
fix/1180-random-prng-seeding

Conversation

@chaliy
Copy link
Copy Markdown
Contributor

@chaliy chaliy commented Apr 11, 2026

Summary

Closes #1180

  • Replace per-access RandomState::new() with a stored LCG PRNG state (AtomicU32) seeded from OS entropy at interpreter creation
  • Use glibc LCG constants (state * 1103515245 + 12345) matching bash behavior
  • Support RANDOM=N reseeding (assigning to RANDOM sets the PRNG seed, consistent with real bash)
  • Different interpreter instances produce different sequences

Test plan

  • test_random_different_instances — two Bash instances produce different first values
  • test_random_reseedRANDOM=42 on two instances yields identical sequences
  • test_random_sequential_varies — three sequential calls produce varying values
  • All 15 random-related tests pass

chaliy added 2 commits April 11, 2026 17:41
…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
@chaliy chaliy merged commit cad12ba into main Apr 11, 2026
27 checks passed
@chaliy chaliy deleted the fix/1180-random-prng-seeding branch April 11, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sec(interpreter): $RANDOM predictability — uses deterministic PRNG without cryptographic seeding

1 participant