Generating Random numbers

October 27th, 2006 mysurface Posted in Bash, echo, Misc, od, touch | Hits: 89794 | 10 Comments »

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.

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 $((...))
For example:

 echo $((5+10))

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.

10 Responses to “Generating Random numbers”

  1. Hi,

    I need to generate 10 random numbers using /dev/random.
    Is this possible ?
    Can you give me syntax for same.
    Also how can i execute this in PHP.

    Thanks in advance

  2. Since when has a computer been able to produce truly random numbers?

  3. Since when have you been able to predict them?

  4. @Deviji:

    PHP has his own functions to generate random numbers.
    Why do you want to generate them in the system?

    check out:

  5. SixDegrees Says:

    “Since when has a computer been able to produce truly random numbers?”

    Read the article. The numbers generated by /dev/random are derived from real-world user input events. Examples would be the time in milliseconds between keystrokes; number of mouse events over some time interval; screen location or time of mouse clicks; and many others.

    There are several articles about /dev/random on the web. The output is, in fact, truly random.

  6. SixDegrees Says:

    “PHP has his own functions to generate random numbers.
    Why do you want to generate them in the system?”

    It depends on whether the numbers are required to be truly random – those generated by PHP are not, while those generated by /dev/random are – or only psuedo-random. In the latter case, PHP’s functions will do just fine.

  7. Momentous weblog, keeping me from updated

  8. Magnificent website. A lot of helpful info here. I am sending it to some buddies ans additionally sharing in delicious. And obviously, thank you on your sweat!

  9. Super helpful, I will send to all my friends on Tweeter, Mybook, and FaceSpace. Be and sure to keep me from updated. Thank you on your info.

  10. Very good web site with good examples.
    For the “random number generator within a range” bear in mind, that the modulo function creates an uneven distribution when the range does not evenly match to the range of random numbers.
    I. e. /dev/random produces a sequence of bytes so the output is base 2.
    Integers are base 10 so a simple modulo operation puts a base 10 grid on a base 2 number.
    This means the intervals at the ends are smaller and thus less likely than the rest.

Leave a Reply