On Unix systems, random numbers are generated in a number of ways and random data can serve many purposes. From simple commands to fairly complex processes, the question “How random is random?” is worth asking.
EZ random numbers
If all you need is a casual list of random numbers, the RANDOM variable is an easy choice. Type “echo $RANDOM” and you’ll get a number between 0 and 32,767 (the largest number that two bytes can hold).
$ echo $RANDOM 29366
Of course, this process is actually providing a “pseudo-random” number. As anyone who thinks about random numbers very often might tell you, numbers generated by a program have a limitation. Programs follow carefully crafted steps, and those steps aren’t even close to being truly random. You can increase the randomness of RANDOM’s value by seeding it (i.e., setting the variable to some initial value). Some just use the current process ID (via $$) for that. Note that for any particular starting point, the subsequent values that $RANDOM provides are quite predictable.
$ RANDOM=$$;echo $RANDOM; echo $RANDOM;echo $RANDOM 7424 28301 30566 $ RANDOM=$$;echo $RANDOM; echo $RANDOM;echo $RANDOM 7424 28301 30566
If you need random numbers fairly frequently, maybe another seed would work better. Here we’re using the number of seconds since the Unix epoch.
$ RANDOM=`date +%s`;echo $RANDOM;echo $RANDOM;echo $RANDOM 32077 1397 32029 $ RANDOM=`date +%s`;echo $RANDOM;echo $RANDOM;echo $RANDOM 16116 16487 11588
You can also use the shuf command to generate pseudo-random numbers. In the command below, we’re generating 10 numbers between 0 and 32,767. The shuf command should start each sequence with a different number (no need for seeding).
$ shuf -i 0-32767 -n 10 32157 16611 24087 28301 9088 4662 12780 30518 7549 12830
More complex random data
For more serious requirements for random data, such as its use in encryption, some more truly random data comes into play. The /dev/random and /dev/urandom files get beyond the predictability of…