Home Learn Linux How to generate random numbers in Bash

How to generate random numbers in Bash

by Pulkit Chandak
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!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.