Skip to content

Produce different $RANDOM sequences in different subshells - #257

Merged
magicant merged 1 commit into
trunkfrom
random
Sep 12, 2026
Merged

magicant merged 1 commit into
trunkfrom
random

Conversation

@magicant

@magicant magicant commented Sep 9, 2026

Copy link
Copy Markdown
Owner

Previously, subshells would inherit the same random number sequence as the parent shell, which could lead to unexpected behavior in scripts that rely on randomness. This change ensures that each subshell generates a different sequence of random numbers.

The sequence is still deterministic if the user seeds $RANDOM to a specific value. When subshells are forked after seeding to the same value, subshells in the same position in the fork tree will yield the same sequence, but other subshells will yield different sequences. This is achieved by saving the given seed in the seed_candidate variable, which is then updated deterministically when a subshell is forked.

Fixes #256

Previously, subshells would inherit the same random number sequence as
the parent shell, which could lead to unexpected behavior in scripts
that rely on randomness. This change ensures that each subshell
generates a different sequence of random numbers.

The sequence is still deterministic if the user seeds $RANDOM to a
specific value. When subshells are forked after seeding to the same
value, subshells in the same position in the fork tree will yield the
same sequence, but other subshells will yield different sequences. This
is achieved by saving the given seed in the `seed_candidate` variable,
which is then updated deterministically when a subshell is forked.

Fixes #256
@magicant
magicant merged commit 76ed58d into trunk Sep 12, 2026
6 checks passed
@magicant
magicant deleted the random branch September 12, 2026 04:29
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.

$RANDOM is not incrimented in subshells

1 participant