In the Bash script, “RANDOM” is the special variable that generates the random numbers. These numbers are utilized in programming languages for generating passwords, playing card games, testing data, etc. However, if the user wants to reproduce the same sequence of random numbers in the bash script, the seed option is considered.
This blog will enlighten how to reproduce the same sequence of random numbers using seed in bash script.
How to Use $RANDOM Variable With Seed in Bash Script?
As mentioned earlier, the seed is used to generate the same random number of sequences. The user can define the “RANDOM” variable with any integer to generate the sequence which ranges between 0 to 32767.
Example 1: Generate Same Output Sequence
To generate the same sequence of random numbers in the bash script, assign a user-defined number to the “RANDOM” is obtained as follows:
#!/bin/bash
RANDOM=32
echo "$RANDOM $RANDOM $RANDOM"
The description of the script is mentioned below:
- Defines the seed value “RANDOM=32”.
- After that, print random numbers via the “echo” command.
Save the file by pressing “Ctrl+O” and exit from the file using “Ctrl+X”.
Run the bash script file in the terminal:
$ bash script.sh
The same number of sequences 13544, 17635, and 3533 are generated for seed value 32.
Example 2: Get Seed Value From User Input
Likewise, the user can also take the seed value and the number of sequences from the user to generate the number of sequences.
To do so, consider the following script:
#!/bin/bash
# Taking input from the user
read -p "Enter Seed Value: " RANDOM
read -p "Enter how many numbers of sequences you want? " range
# Generate random numbers
for (( i=1; i<=$range; i++ ))
do
echo $RANDOM
done
The script is defined as below:
- First, input the seed value from the user and store it in the “RANDOM” and the number of sequences in the “range” variable.
- After that, the “for“ loop is utilized to define the range and print the random numbers on the screen.
Save the above script in the file.
Run the script file using the bash command as shown:
$ bash script.sh
The same number of sequences 23686, 19043, and 26511 are displayed after entering seed value 56 and range 3.
Example 3: Get Range, Seed Value, and Number of Sequences from the User
To retrieve the range, seed value, and the number of sequences from the user, run the below-mentioned script:
#!/bin/bash
#taking input from the user
read -p "Enter Maximum Value: " max
read -p "ENter Minimum Value: " min
read -p "Enter Seed Value:" RANDOM
read -p "How many Numbers you want Generate:" number
#Checking the Entered Numbers
if [[ $max -lt $min ]];
then
echo "Sorry the Maximum value is less than Minimum Value"
exit 1
fi
#Check Range
Range=$(($max-$min))
if [[ $Range -eq 1 ]];
then
echo "The Range between 2 numbers should be more than 1"
exit 1
fi
#for loop to generate Random numbers
for (( i=1; i<=$number; i++ ))
do
random_no=$(($RANDOM % $max + $min))
echo "Random Number is $random_no"
done
The script is defined as
- Take the Range in “max”, and “min” variable from the user.
- Then take the seed value and the number of sequences to generate from the user in the “RANDOM” and “number” variables.
- “if” statements validate the “max” and “min” values entered by the user and the range between the values.
- The “for“ loop defines the number of sequences using the “number” variable.
- After that, the “do” section generates the random number sequence and stores it in the “random_no” variable.
- Then, the echo command prints generated random numbers on the screen.
Save the above script in the file.
Let’s execute the above script in the terminal:
$ bash script.sh
The same numbers 25, 14, and 46 are generated after inputting 45 as maximum, 4 as minimum values, and 78 as a seed value.
Conclusion
In the Bash script, the seed value is defined in the “RANDOM” variable to generate the same number of sequences. This value can be defined manually or by the user input. Users can print these values in the terminal via the “echo” command.
This write-up has answered the query “what is RANDOM with seed in bash” with the help of examples.