How to Generate Random Number in Bash

  1. Using the $RANDOM Variable to Generate Random Numbers
  2. Using the shuf Command to Generate Random Numbers
  3. Using /dev/urandom to Generate Random Numbers
  4. Using Awk to Generate Random Numbers
  5. Conclusion
  6. FAQ
How to Generate Random Number in Bash

Generating random numbers is a common task in programming, and Bash provides a straightforward way to do this. Whether you’re writing scripts for automation, testing, or even games, knowing how to generate random numbers can be incredibly useful. In this tutorial, we will delve into the various methods available in Bash to generate random numbers efficiently.

From simple one-liners to more complex approaches, this article will guide you through each method step by step. By the end, you will have a solid understanding of how to generate random numbers in Bash, which can enhance your scripting capabilities significantly.

Using the $RANDOM Variable to Generate Random Numbers

The $RANDOM variable can be used to produce a random number or a range of random values between 0 and 32767. However, you may limit the number range used to generate random numbers by dividing the $RANDOM value by a specific value.

To produce a random number between 0 and 32767, use the following command:

$ echo $RANDOM

Output:

28091

You may produce a random number from a specific range by dividing the $RANDOM variable by the remaining value. It contains double first parentheses (()) with a $ symbol.

To produce a random number between 1 and 100, use the following command:

$ echo $(( $RANDOM % 100 + 1 ))

Output:

19

Since the percentage sign causes the mathematical operation modulo to fail, the number 100 is never attained (remainder after division). If the random generator returned the value 100, the modulo operation would provide the value 0.

Using the shuf Command to Generate Random Numbers

You may use the command shuf to produce numerous numbers in a shell script.

The following command will create a single integer between 0 and 100:

$ shuf -i 0-100 -n1

Output:

76

In the above command, -i denotes the input range, and -n denotes the headcount. The key-n is followed by any positive number, indicating the number to generate random numbers. Run the following command to print five random numbers between 0 and 100.

$ shuf -i 0-100 -n5

Output:

68
78
24
76
41

Using /dev/urandom to Generate Random Numbers

The /dev/urandom device file allows you to produce pseudo-random numbers far more random than the $RANDOM variable. However, putting these numbers into variables in a script involves further work, such as filtering via od, as shown in the example.

od /dev/urandom  -A n -t d -N 1

The -t d option denotes that signed decimal should be used as the output format, and -N 1 tells the program to get one byte from /dev/urandom.

Output:

124

Using Awk to Generate Random Numbers

The rand() function of awk returns a random floating-point number in the range of 0 - 1. The commands that follow BEGIN will be executed before the next step.

awk 'BEGIN{x=rand(); print x}'

Output:

0.444937

Conclusion

Generating random numbers in Bash is a straightforward process that can be accomplished using various methods. Whether you prefer the simplicity of the $RANDOM variable, the quality of randomness from /dev/urandom, or the flexibility of custom functions, Bash provides the tools you need. By mastering these techniques, you can enhance your scripting capabilities and address a wide range of programming challenges with ease.

FAQ

  1. How does the $RANDOM variable work in Bash?
    The $RANDOM variable generates a random integer between 0 and 32767 each time it is referenced.

  2. Can I generate random numbers in a specific range using $RANDOM?
    Yes, you can use the modulus operator to limit the output to a specific range.

  3. What is /dev/urandom?
    /dev/urandom is a special file that provides a stream of random bytes, which can be used to generate high-quality random numbers.

  4. How can I create a reusable function for random number generation in Bash?
    You can define a function that takes minimum and maximum values as arguments and returns a random number within that range.

  5. Is there a difference between /dev/random and /dev/urandom?
    Yes, /dev/random blocks when there is not enough entropy, while /dev/urandom does not block and is generally faster.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe