How to generate random numbers in Bash

Random numbers have a wide range of applications all across programming. Testing for functionality is a big part of it. Generating unique IDs, passwords, or other random strings of characters is another big use case. There are several ways of generating random numbers in Bash, and today we will check them out.

Generating random numbers in Bash

Using the shuf command

The shuf command can be used to create permutations of random characters. Here, we will only see the option to generate a random integer. If you need to generate between A and B, and C number of numbers are required, the command becomes:

shuf -i A-B -nC

So, for example, generating three numbers between 10 and 40:

shuf -i 10-40 -n3
Using shuf command to generate random numbers
Using shuf command to generate random numbers

Using /dev/urandom

As we look into other methods, we get deeper into the working of Linux. The /dev/urandom file is a file that keeps collects random environmental noise from device drivers and creates an entropy pool, which is used to create random numbers. Since this is not a normal file, it needs to be used through some other commands to be read. We will use the od command, which is used to dump files in different formats. The ranging method is quite different in this case, though. It works on the number of bytes:

od /dev/urandom -A n -t d -N 1
Using /dev/urandom to generate a random number
Using /dev/urandom to generate a random number

This command will generate a number that will not exceed 255 because that is the maximum size of one byte.

Using the $RANDOM variable

The most direct way that Bash provides to generate a random number is the $RANDOM variable. Unlike most variables with a constant value, and as the name suggests, echoing this variable gives a random number between 0 and 32767:

echo $RANDOM

But the issue is that, more often than not, we need a random number within a specific range. There is a very clever workaround to make this variable work that way. Say you want the number to be between A and B; the command would look like this:

echo $[ $[ $RANDOM % $[ B-A+1] ] + A ]

To break this down, let us take an example. Say we want a random number between 10 and 40. We can think of it another way: generate a random number between 0 and 30 and add 10. Hence, if we generate a random number in the range spanned by the two required boundaries (here, 40-10 = 30) and add it to the lower bound (random number between 0 and 30, but shifted 10 places higher), we can get a random number in the range we want.

How do you get a random number between 0 and some number? That is relatively easier. We use the remainder. Here, we want a number between 0 and 30. For whatever random number is generated, if we calculate its remainder with 30, we will get a number between 0 and 29. But wait, we want a number between 0 and 30. So what if we calculate the remainder with 31? That way, we get a proper range between 0 and 30.

So finally, we divide the random number by the range spanned by the boundary numbers + 1, and we shift it up by the lower boundary. All in all, it gives the formula mentioned above. So in our case of a random number between 10 and 40, the command becomes:

echo $[ $[ $RANDOM % $[ 40-10+1] ] + 10 ]
Using $RANDOM variable
Using the $RANDOM variable to generate a random number

Since %RANDOM is only a variable, there isn’t much more to explore. It is just a number, so your imagination is the limit with mathematical operations. Nevertheless, you can mold it to suit whatever you have in mind.

Conclusion

As we have seen, there are several ways of creating random numbers in Bash. Some are more ubiquitous than the rest, and some are simpler than the rest. In any case, Bash will have you covered for whatever requirement you might have. We hope this article was helpful to you. Cheers!

Print Friendly, PDF & Email

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Server Status

Aradippou Chat 1 Chat 2 Larnaca Limassol Nicosia

Chat Links

Official Links.

1. Chat 1.
2. Chat 2.

Alternative Mirror Links.

1. KiwiIRC 1.
2. KiwiIRC 2.

Other Web Clients.

1. IrcCloud.

Recent Posts

Related Posts:

Advertisement

Archives

Super Club Radio

Mighty Deals

CyIRC

CyIRC Tweets

Scroll Up
Fatal error: Uncaught WMAC\JSMin_UnterminatedStringException: WMAC\JSMin: Unterminated String at byte 1181: "+o.accessToken}}).then(e=>{if(!e.ok)throw e;return e});return a.catch(e=>{console.error(`Failed to logout chatrix session. deviceId: ${o.deviceId}`)}),a}})(); in /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php:215 Stack trace: #0 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php(157): WMAC\JSMin->action(2) #1 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php(96): WMAC\JSMin->min() #2 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/class-main.php(375): WMAC\JSMin::minify('(function(){"us...') #3 /home/bszucbfe/public_html/wp-includes/class-wp-hook.php(324): WMAC_PluginMain->jsSnippetcacher('(function(){"us...', '/home/bszucbfe/...') #4 /home/bszucbfe/public_h in /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php on line 215