We can obtain pure random (NOT pseudo-random) bytes from /dev/random. Linux kernel harnesses a good source of randomness from you. The random bytes in /dev/random is measuring based on the time delay between your input actions.
so seems /dev/random provided random bytes, we need to turn the bytes into an integer so that we could read. The simplest way is uses
od is a kind of object dump tools, we can specified how many btye to extract from a file, what is the type of output ( decimal, floating points, hexadecimal etc).
The example bellow shows the simple way of extract a 2 byte decimal integer using
od from /dev/random.
od -An -N2 -i /dev/random
-An is to turn off the address from displaying, -N specified number of bytes to extract, -i indicate display format is an integer.
Okay now what? Let say we want to create a file with random number as a file name with “.tmp” as the extension. I simply uses
touch to create a empty file.
touch `od -An -N2 -i /dev/random`.tmp
Let say I want to have a random number within a range. Let say from 100 to 1000.
echo $(( 100+(`od -An -N2 -i /dev/random` )%(1000-100+1) ))
It looks complicated, but actually it is not. To do a simple calculation at bash shell, you can put your equation in
The the formula for getting a random number in range is
min+( seed%(max - min + 1) ) .
/dev/random provide quite number of bytes of random value, if you need even larger, you can obtain at /dev/urandom.