Get Random Number Between 1 and 100 in Bash

Get Random number between 1 and 100 in Bash

Using $RANDOM Variable

Use the $RANDOM variable to generate a random number between 1 and 100 in Bash.

The above code generates a random integer number between 1 to 100 and prints it on the bash console. How?

The $RANDOM generated a random integer between 0 and 32767. Then, we used the %100 to calculate the remainder of $RANDOM/100 to get a random value between 0 and 99; however, 1 was added to make it 1 to 100.

The $((...)) is an arithmetic expression that we used to evaluate an expression inside the parentheses (()) and got the result as an integer which was printed on the console using echo.

Using awk with rand() and srand()

Use the awk command to generate a random number between 1 and 100 in Bash.

Here, we used a command line tool, awk, to generate a random integer between 1 and 100 (inclusive) and printed it to the console using the print statement. The point is how this code worked.

Let’s start with awk, which is used for text processing and manipulation and works by reading specified input files line by line, processing every line and displaying the results on the console.

The BEGIN is a particular pattern in awk that specifies a code block to be executed before any input is processed. In the above case, the BEGIN was used to initialize a random integer number generator with the srand() method, which initialized the random number generator with a seed value. Remember, if it does not get any seed value, this function will use the current time as a seed.

We used another function, rand(); this function generated a random floating-point number between 0 and 1 (exclusive). Multiplying this number with 100provided us a random number between 0 and 100 (exclusive) while the int() method rounded it down to the closest integer number. Finally, we added 1 to the result to grab a random integer number between 1 and 100 (inclusive) and printed it on the console.

Using shuf Command

Use the shuf command to generate a random number between 1 and 100 in Bash.

Here, we used the shuf command-line utility to generate a random permutation of the given input. In the above code, shuf was used with the -i option to produce a random sequence of integer numbers between 1 and 100 (1-100).

We used the -n option to mention the number of random integers generated; if you want to generate three random integers, you can replace the value 1 with 3 for the -n option. See the following example.

Finally, the output produced by the shuf command was passed as an argument to the echo command using $(...) (command substitution) to print it on the console.

Using /dev/urandom Device File

Use the /dev/urandom device file to generate a random number between 1 and 100 in Bash.

We used the od and awk commands to generate a random integral value between 1 and 100 (inclusive).

The od command dumped the file’s content, or in the above case, /dev/urandom, a particular file in a Unix-like OS providing a limitless stream of random bytes.

Next, the -An option informed the od to not print each byte’s address while -N2 specified the number of bytes to be dumped, which was 2 in the above code. Finally, the -i option lets the od option output integers instead of the default hexadecimal format, which means the od will produce 2 random bytes as signed 2-byte integers in little-endian byte order.

Finally, the od‘s output was piped to the awk command, which read the input from od line by line and applied the action block (enclosed in {}) for every line. What did this action block do?

The awk command used % (modulo operator) to retrieve the remainder of the first field of the input line/100. As the input line contained one integer, $1 referred to that value, while 1 was added to get the number between 1 and 100 (inclusive). Finally, the awk printed the value on the console using the print statement.

Using for Loop

Use the for loop to generate a random number 10 times between 1 and 100 in Bash.

Using the for loop, we can generate as many numbers as we want between 1 and 100 (inclusive) because we used the seq command to create a number that will be used as input for for loop, in the above code, we got a sequence from 1 to 10.

In the loop, we used the $RANDOM variable to generate random integer numbers, which we have already learned in the first section that you can refer to.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *