Bash or BourneAgain SHell is a commandline interpreter included in most Linux distributions. It provides a Unixlike commandline environment for executing shell scripts and commands. One of the common tasks in Bash scripting is generating random numbers. Random numbers are often used in programming, especially in simulations, games, and cryptography. This article will discuss how to generate random numbers in Bash and provide code examples.
PseudoRandom Numbers in Bash
In computer science, random numbers are not truly random, but they are generated based on some mathematical algorithms. Pseudorandom numbers are the result of deterministic algorithms that approximate randomness. In Bash, the $RANDOM variable can produce a pseudorandom integer between 0 and 32767.
Code Example:
The following code generates a random number between 0 and 32767 and prints it to the standard output:
#!/bin/bash
echo $RANDOM
The output will be a random integer between 0 and 32767.
Real Random Numbers in Bash
Pseudorandom numbers are not suitable for cryptographic purposes or applications that require true randomness. They are also not truly random because they are generated based on some algorithm. On the other hand, real random numbers come from physical sources that generate unpredictable values, such as atmospheric noise, radioactive decay, and others.
The random number generator (RNG) utility is one of the ways to generate real random numbers in Bash. It generates random numbers from random data files in the /dev directory. It reads data from hardware devices (e.g., mouse, keyboard, network card) or kernel input queues, and then processes the data to generate a real random number. The /dev/random file generates real random numbers based on the entropy pool, which collects random data from various sources.
Code Example:
The following code generates a real random number between 0 and 32767 using the RNG utility in Bash:
#!/bin/bash
cat /dev/random  tr dc '09'  fold w 5  head n 1
The cat
command reads the data from the /dev/random file. The tr
command deletes any nonnumeric characters and leaves only digits. The fold
command formats the output into lines of five digits each. The head
command selects the first line of output.
Random Numbers in a Range in Bash
Often, you need to generate a random number within a specific range, not just between 0 and some maximum value. For example, you may need to generate a random number between 1 and 6 for a dice game.
One way to generate a random number within a range is to use the modulo operator with the maximum bound. You can then add the minimum value to the result to shift the random number to the desired range.
Code Example:
The following code generates a random number between 1 and 6 (inclusive) in Bash:
#!/bin/bash
min=1
max=6
range=$(($max$min+1))
result=$(($RANDOM%$range+$min))
echo $result
The min
and max
variables specify the minimum and maximum values of the range. The range
variable calculates the size of the range by subtracting the minimum value from the maximum value and adding 1. The result
variable generates a random number by using the modulo operator with the range and adding the minimum value. The result will be a random integer between 1 and 6.
Conclusion
Generating random numbers is a common task in programming, including Bash scripting. Bash provides two methods of generating random numbers: pseudorandom and real random. Pseudorandom numbers are generated by an algorithm, while real random numbers come from unpredictable sources. The RNG utility in Bash can generate real random numbers, while the $RANDOM variable generates pseudorandom numbers. You can also generate random numbers within a specific range by using the modulo operator and adding the minimum value.
Generating Random Numbers in Bash
Random number generation is a crucial part of many programming tasks, including simulations, games, and cryptography. In Bash, you can generate random numbers in several ways, as discussed earlier. The most common way is by using the $RANDOM variable, which produces pseudorandom numbers between 0 and 32767.
In addition to the $RANDOM variable, you can use the RNG utility to generate real random numbers based on physical sources of entropy such as atmospheric noise. The RNG utility reads data from the /dev/random file and processes it to generate a random number.
The /dev/random file is considered to be a source of true randomness and more secure than the $RANDOM variable. However, the downside of using /dev/random is that it may block if it runs out of the entropy pool. To prevent this, you can use the /dev/urandom file, which generates pseudorandom numbers based on the same data sources as /dev/random and never blocks.
Generating Random Numbers in a Range
Generating random numbers within a specific range is also a common requirement, as discussed earlier. One way to do this is by using the modulo operator with the range's size and adding the minimum value to the result. This method ensures that the random number falls within the specified range.
Another way to generate random numbers within a range is by using the shuf command. The shuf command randomly shuffles input lines and outputs them to the standard output. You can generate a list of numbers within a range and then use the shuf command to shuffle the list and select a random number from it.
Code Example:
The following code generates a random number between 1 and 6 using the shuf command:
#!/bin/bash
seq 1 6  shuf n 1
The seq
command generates a list of numbers from 1 to 6, and the shuf
command shuffles the list and selects one number from it.
Conclusion
Random number generation is an essential part of programming, and Bash provides various methods to generate random numbers. The $RANDOM variable produces pseudorandom numbers, while the RNG utility generates real random numbers based on physical sources of entropy.
To generate random numbers within a specific range, you can use the modulo operator and add the minimum value to the result. You can also use the shuf command to generate a list of numbers within the range and select a random number from it.
Regardless of the method you use, generating random numbers in Bash is simple and efficient, and it can help you enhance your scripts and applications.
Popular questions

What is the $RANDOM variable, and how does it generate random numbers in Bash?
Answer: The $RANDOM variable is a system variable in Bash that generates a pseudorandom integer between 0 and 32767. It generates random numbers by using a deterministic algorithm that produces a different result each time it is called. 
How can you generate real random numbers in Bash, and why are they useful?
Answer: You can generate real random numbers in Bash by using the RNG utility that reads data from /dev/random file. Real random numbers are useful because they come from natural and unpredictable sources of entropy and are more secure for cryptographic applications. 
How can you generate random numbers within a given range in Bash using the modulo operator?
Answer: You can generate random numbers within a given range in Bash by using the modulo operator to compute the remainder of a division operation. By taking the remainder with the size of the range and adding the minimum value, you can shift the range to the desired values. For example, to generate a random number between 1 and 6, you can use the following code:result=$(($RANDOM % 6 + 1))
. 
What is the shuf command, and how can you use it to generate random numbers?
Answer: The shuf command is a POSIX utility that randomly shuffles the lines of an input file or standard input and outputs the results to standard output. You can use it to generate random numbers by first creating a list of integers and then using the shuf command to select a random number from that list. For example:seq 1 6  shuf n 1
. 
What is the difference between pseudorandom and real random numbers, and why is it important to distinguish between them?
Answer: Pseudorandom numbers are generated by deterministic algorithms and are not truly random, while real random numbers come from physical sources of entropy and are closer to true randomness. It is important to distinguish between them because real random numbers are more secure for cryptographic applications and are better suited for simulations and games that require natural randomness.
Tag
Randomizer