Unix Shell - Why are the same $RANDOM numbers repeated? Unix Shell - Why are the same $RANDOM numbers repeated? unix unix

Unix Shell - Why are the same $RANDOM numbers repeated?


This is due to a zsh bug / "behaviour" for RANDOM in subshells. This bug doesn't appear in bash.

echo $RANDOM # changes at every run  echo `echo $RANDOM` # always return the same value until you call the first line

Because RANDOM is seeded by its last value, but in a subshell the value obtained is not updated in the main shell.

In man zshparam:

RANDOM <S>A  pseudo-random  integer  from 0 to 32767, newly generated eachtime this parameter is referenced.  The random number  generatorcan be seeded by assigning a numeric value to RANDOM.The   values   of   RANDOM   form   an  intentionally-repeatablepseudo-random sequence; subshells  that  reference  RANDOM  willresult  in  identical  pseudo-random  values unless the value ofRANDOM is referenced or seeded in the parent  shell  in  betweensubshell invocations.

There is even crazier because calling uniq creates a subshell

for i in {1..10}; do echo $RANDOM; done # changes at every run for i in {1..10}; do echo $RANDOM; done | uniq # always the same 10 numbers

Source : Debian bug report 828180


Pseudorandom number generators are not perfect. The Lehmer random number generator is used in bash sources with the "standard" constants:

x(n+1) = 16807 * x(n) mod (2**31 - 1)

moreover bash limits the output to 15 bits only:

#  define BASH_RAND_MAX 32767...return ((unsigned int)(rseed & BASH_RAND_MAX));

With the seed your shell has been seeded, it just so happens that numbers 4455 and 4117 appear one after another in consecutive output of 10000 random numbers. Nothing surprising there really. You could calculate the seed to get two consecutive numbers knowing that:

# We know that lower 15 bits of previous number are equal to 4455x(n) mod 32768 = 4455# We know that lower 15 bits of previous number are equal to 4455x(n+1) mod 32768 = 4455# We know the relation between next and previous numberx(n+1) = 16807 * x(n) mod (2**31 - 1)# You could find x(n)

Why are the same $RANDOM numbers repeated?

Because the used pseudorandom generator method in bash sources with the current seed in your shell happens to repeat the same number.