Use of random generator:
The random number or a range of random numbers can be generated using the $RANDOM variable. It generates a random number between 0 and 32767 by default. But you can set the range of numbers for generating random numbers by dividing the value of $RANDOM with a specific value. Different uses of the $RANDOM variable to generate random numbers are shown in the tutorial’s next part.
Random number generation using $RANDOM variable:
The ways to generate the random number in the terminal and execute a script file are shown here.
A. Generate Random numbers from the terminal
Run the following command to generate a random number within the range 0 to 32767.
You can generate a random number of a specific range by dividing the $RANDOM variable with a particular value and getting the remainder value. Run the following command to generate a random number within the range of 1 to 50. Here, double first brackets with $ have been used.
Run the following command to generate a random number within the range of 10 to 40. Here, the third bracket with $ has been used.
B. Generate Random numbers using the script
Create a bash file with the following script to generate a random number of the specific range where the minimum and maximum range values will be taken from the user. An error message will be displayed if the taken maximum value is smaller than the minimum value. If the difference between the maximum and the minimum value is 1, another error message will be displayed. A random number will be generated in each execution of this script if the valid minimum and maximum values will be taken as input.
# Generate a randomly based range defined by the user
#Take the lower and the upper value from the user
echo "Enter the minimum value:"
read minimum
echo "Enter the maximum value:"
read maximum
#Check the taken values are valid
if [[ $maximum < $minimum ]]; then
echo "Maximum value can't be lower than minimum value"
exit 1
fi
#Find out the difference between the numbers
diff=$(($maximum-$minimum))
#Check the difference value
if [[ $diff == 1 ]]; then
echo "The range of numbers must be more than 1"
exit 1
fi
#Generate the random number
randomNumber=$(($minimum + $RANDOM % $maximum))
#Print the generated number
echo "The generated random number is: $randomNumber"
The following output will appear if the script is executed multiple times. Here, the above script has been executed three times. The error message has been printed for the first two executions for invalid input, and a random number has been generated for the last execution.
Random number generation using `shuf` command:
Using the `shuf` command is another way to generate the random number of a specific range. The ways to generate a random number from the terminal and use a script have been shown in this tutorial.
A. Generate Random numbers from the terminal
Run the following command to generate a random number between 0 to 50 using the `shuf` command.
According to the following output, the above command has been executed three times, and three random numbers have been generated.
B. Generate Random numbers using the script
Create a bash file with the following script to generate a list of random numbers based on the input value. The `for` loop has been used to execute the `shuf` command multiple times to generate the list of random numbers between 1 to 100 and print the numbers.
# Generate a random using `shuf` command
echo "How many random numbers do you want to generate?:"
read number
#Print the generated random numbers
echo "The generated random numbers are:"
for n in `seq "$number"`
do
randomNumber=$(shuf -i 1-100 -n1)
echo $randomNumber
done
The following output shows that 5 has been taken as the input value, and 5 random numbers have been generated, which are not more than 100 and not less than 1.
Random number generation using /dev/urandom:
The /dev/urandom can be used with different commands to generate different types of random values. It can’t be used to specify the range values like the `shuf` command and $RANDOM variable. But the number of the digits of the random number can be defined in command with /dev/urandom. The use of the `od` command with /dev/urandom has shown in the next part of this tutorial. This command can be used to specify the number of bytes where each byte can be defined by a decimal number within 0 to 255.
Run the following command to generate a random number between 0 and 255.
The output shows that the above command has been executed three times, and three different random numbers have been generated here where the values are not more than 255.
Conclusion:
Three different ways to generate random numbers have been explained in this tutorial by using various examples. The coder can generate a specific range of random numbers by using the $RANDOM variable or `shuf` command in bash. The coder can use /dev/urandom with any other command to generate a random number of particular bytes or lengths. Generating random numbers is a very common requirement for programming, and I hope the readers will be able to generate a random number based on their requirements after reading this tutorial.