7

I am working on a bash script that uses the $RANDOM environmental variable as an input in a simulation. The variable does what it say, give random integers, and I as far I understand it is taken from /dev/random.

However I would like to have a reproducible simulation, then the pseudo-random generator should be initialized with a seed; is it possible to have a seed for the $RANDOM variable in bash?

Juan
  • 1,520
  • 2
  • 19
  • 31

1 Answers1

19

From the man page:

   RANDOM Each time this parameter is referenced, a random integer between
          0 and 32767 is generated.  The sequence of random numbers may be
          initialized by assigning a value to RANDOM.  If RANDOM is unset,
          it loses its special properties,  even  if  it  is  subsequently
          reset.

Note that assigning a value to RANDOM actually seeds it; the assigned value won't be the next value returned.

$ RANDOM=1341
$ echo $RANDOM $RANDOM $RANDOM
26571 16669 28842
$ echo $RANDOM $RANDOM $RANDOM
14953 18116 2765
$ RANDOM=1341
$ echo $RANDOM $RANDOM $RANDOM
26571 16669 28842
$ echo $RANDOM $RANDOM $RANDOM
14953 18116 2765
chepner
  • 497,756
  • 71
  • 530
  • 681