The random numbers represent a group of numbers that are extracted arbitrarily from a sequence/set of numbers. Most of the random number generators offer the same generation and prediction probability.  In certain situations, users must generate random numbers in a specific range where unpredictability and randomness are important. These numbers have various applications, such as generating passwords, simulation, modeling, encryption, and security.

This guide will offer all possible methods to generate random numbers within a particular range in Linux.

## Method 1: Using the “shuf” Command

The “shuf” command is utilized to generate random permutations of input lines. By default, it generates random permutations of all input lines.

Here’s the basic syntax of the “shuf” command to generate random numbers in a specific range:

```\$ shuf -i START-END -n COUNT
```

In the above syntax, “START” and “END” define the range of numbers that users want to generate. After that, the “COUNT” specifies the number of random numbers to generate.

Example:

For instance, use the “shuf” command with the “i” option to generate 5 random numbers between 1 and 10 in the following command:

```\$ shuf -i 1-10 -n 5
```

This command generates 5 random numbers between 1 and 10 and prints them on separate lines.

## Method 2: Using the \$RANDOM Variable

In the Bash shell, users can utilize the \$RANDOM variable for generating random numbers within 0 and 32767. To generate a random number in a specific range, you can use the following formula:

```\$ (((\$RANDOM % (max - min + 1)) + min))
```

Here, “min” is the minimum value that users want to generate and “max” is the maximum value. The expression \$RANDOM % (max – min + 1) generates a random number within 0 and max-min. Adding min to this result gives a random number within min and max. The double parentheses are used to evaluate the arithmetic expression.

Example:

To generate random numbers within a specific range, use arithmetic operations to scale and shift the range of the numbers. In our case, generate random numbers between 1 and 100 via the following command:

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

The above command generates a random number “72” between 0 and 99 via the \$RANDOM variable after adding 1 to it and prints the result.

## Method 3: Using the jot Command

The jot command is a utility available in some Linux distributions that can generate random numbers in a specific range. To generate random numbers within a specific range, execute the jot command by following the syntax:

```\$ jot -r count min max
```

The syntax’s components are:

• “-r” specifies that the numbers should be randomly generated.
• count” is the random number to generate.
• min” is the minimum range value.
• max” is the maximum range value.

Example

To generate 5 random numbers between 1 and 10, use the “jot” command with the “r” option by specifying the range and count of random numbers as below:

```\$ jot -r 5 1 10
```

This command generates 5 random numbers between 1 and 10 in the separated lines.

Note: If the “jot” utility is not installed, users can execute the “sudo apt install athena-jot” command for Ubuntu, “sudo yum install athena-jot” for CenOS, and “sudo dnf install athena-jot” for Fedora-based Linux Distributions.

## Conclusion

To generate random numbers in a specific range, use the “shuf” command, “\$RANDOM” variable, and “jot” command. All these above methods require a range of numbers to generate random numbers. Furthermore, users can specify count numbers to generate random numbers.

This guide has demonstrated several ways to generate random numbers in a specific range in Linux.