Generating random numbers in UNIX shell:
$ RANDOM=12345
$ echo $RANDOM
5758
$ echo $RANDOM
10207
$ echo $RANDOM
7212
$ echo $RANDOM
25339To "seed" the RANDOM number (i.e., to get it started), you first initialize it (typically with an odd number -- or even better with a prime -- though usually the writers of the code use a prime base, whence primality of the seed is less relevant). Initialization with the same number each time will give the same sequence of random numbers (which can be useful for debugging, since you may be able to track down where a program went awry). If you want differing sequences of random numbers it is common to use a call to the system clock with a statement such as
$ date +%N
443892000which returns the current time in nanoseconds (usually reported with accuracy on the order of microseconds, depending on the version of UNIX -- thanks to Dr. Conlon for helping me make sense out of the man file on date).
Putting it all together, we might do something like:
$ RANDOM=`date +%N|sed s/...$//`
$ echo $RANDOM
6599
$ echo $RANDOM
686
$ echo $RANDOM
22505It appears (based on naive observation of its behavior, together with a bit of conjecture) that $RANDOM will be in the interval 0 to 32,767. Hence you may have to do some arithmetic to adjust the size of the numbers you are dealing with:
$ echo "scale=9; $RANDOM/32767"|bc
.860866115
Another approach would be to use awk:
$ echo|awk '{print rand()}' 0.237788Or, better yetecho|awk 'srand() {print rand()}'And you may want to look at this:$ echo -e "\n\n"|awk '{print rand()}'0.237788
0.291066
0.845814