Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Bash’s $RANDOM not seeded (?) on some machines?

Tags:

bash

random

seed

I noticed that on some machines (all openSUSE 11.2 on identical hardware) every Bash shell would output the same sequence of values for $RANDOM:

$ bash -c 'for i in `seq 10`; do echo -n "$RANDOM "; done; echo'
17767 9158 6249 18547 23633 23807 5194 22764 7977 31949 
$ bash -c 'for i in `seq 10`; do echo -n "$RANDOM "; done; echo'
17767 9158 6249 18547 23633 23807 5194 22764 7977 31949

The sequence is the same across all these machines. It seems that the random number generator is not seeded? Why does it happen and how to fix it?

On my personal machine the numbers are different every time I call above command.

like image 565
hfs Avatar asked Mar 13 '11 16:03

hfs


1 Answers

You can just seed it:

bash -c 'RANDOM=$$; for i in `seq 10`; do echo -n "$RANDOM "; done; echo'

bash -c 'RANDOM=`date +%s`; for i in `seq 10`; do echo -n "$RANDOM "; done; echo'
like image 166
Erik Avatar answered Sep 22 '22 16:09

Erik