Generate Random Numbers In Terminal/Bash 0

Terminal

Share
Generate Random Numbers In Terminal/Bash

Hey

I have a previously discussed about generating random numbers in applescript, today I am going to discuss two methods which you can use to generate random numbers in Terminal. This is useful for bash, terminal or any other scripts which use the language.  The two methods allow you to have a certain element of control on the size and the randomness and type of number produced. There are certain levels of randomness with random numbers produced by computers. One of the options shown will use an aspect of your Mac which is very random. If you don’t want the book versions of a Million Random Digits, using a computer is probably better.

Option One

This first options is rather simple, and great if you want a simple number for your script. Simply type the following into Terminal or your bash script, note that it has to be written in capitals:

echo $RANDOM

This will output a number up to 32,767, for those interested it is a signed halfword noted for being 16 bits in length. If you want to limit the random number up to a certain length simply type the following:

echo $[RANDOM%X+1]

Where X is your largest number you want, although it has to be less than 32,767. If you want a longer random number you can simply repeat the $RANDOM section of the command, such as the following:

echo $RANDOM$RANDOM

Using a combination of the previous two commands you can tailor the size of the number to anything you want. Although this method is simple and for most cases satisfactory I am unsure on the complete randomness of the number. The second command will have more randomness, although the first option is based on this command.

Option Two

This second command will use something called /dev/urandom, which is based on something called /dev/random. It takes noise in your system, such as mouse movements, network cards, hard drive access to name a few and output the random data to /dev/random and dev/urandom. We can then take the random 0’s and 1’s produced and convert them into a numerical number which we can use. This commands has slightly more control over the size and types of numbers which we produce.

The command to generate this random number is as follows:

od -vAn -N4 -tu < /dev/urandom

This will generate a large number which you can use as your random number. There are two options which I would like to draw your attention to, although you can read about the od command, which is used to output the number on its man page, the two options which are best changed is the number following the capital N, and the letter u between the t and 4 within the command options.

The -N options is used to control the size of the number in bytes, the number four will produce a ten digit number. If you increase the size of this number, eg to 5, it will split the outputted number into a 4 byte, 10 digit number and then into a one byte number. If you remove this option completely from the command, it will keep producing numbers.

The next option is to change the -tu4 to something different. You can change the letter u to either, d, o, or x. The letter d will produce a signed decimal to produce a value from − 2,147,483,648 to 2,147,483,647. The letter o will produce an octal value, where as u will produced an unsigned value of 0 to 4,294,967,295. Finally x will produce a hexadecimal value. For example:

od -vAn -N4 -tx < /dev/urandom

Produces the output, 05f70be9. Neat.

Conclusion

To conclude. The first option, is quick and easy. It allows you to generate a number and input them into a script will little thinking. The second option is more complex but allows you to have more control over the final output. Certainly more efficent than buying a book with random numbers.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips