0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Generate Random Number in Range

Use random_number=$((RANDOM % (max - min + 1) + min)) in Bash to generate a random number between min and max inclusive.
📋

Examples

Inputmin=1, max=5
Outputrandom_number between 1 and 5, e.g., 3
Inputmin=10, max=10
Outputrandom_number is always 10
Inputmin=0, max=0
Outputrandom_number is always 0
🧠

How to Think About It

To generate a random number in a range, first find the size of the range by subtracting the minimum from the maximum and adding one. Then use the Bash built-in RANDOM which gives a number from 0 to 32767, take the remainder when divided by the range size, and add the minimum to shift it into the desired range.
📐

Algorithm

1
Get the minimum and maximum values for the range
2
Calculate the size of the range as (max - min + 1)
3
Generate a random number using Bash's RANDOM variable
4
Use modulo operator to limit the random number within the range size
5
Add the minimum value to shift the number into the desired range
6
Return or print the resulting random number
💻

Code

bash
#!/bin/bash
min=5
max=15
random_number=$((RANDOM % (max - min + 1) + min))
echo "Random number between $min and $max: $random_number"
Output
Random number between 5 and 15: 11
🔍

Dry Run

Let's trace generating a random number between 5 and 15 through the code

1

Set min and max

min=5, max=15

2

Calculate range size

range = 15 - 5 + 1 = 11

3

Generate RANDOM and apply modulo

Assume RANDOM=12345; 12345 % 11 = 10

4

Add min to shift range

10 + 5 = 15

StepRANDOMModulo ResultFinal Number
1---
2---
31234510-
4--15
💡

Why This Works

Step 1: Using RANDOM

Bash's RANDOM gives a pseudo-random integer between 0 and 32767 each time it is accessed.

Step 2: Limiting range with modulo

Using % (max - min + 1) limits the random number to the size of the desired range.

Step 3: Shifting to desired range

Adding min shifts the number so it starts from the minimum value instead of zero.

🔄

Alternative Approaches

Using /dev/urandom and head
bash
#!/bin/bash
min=5
max=15
range=$((max - min + 1))
random_number=$(od -An -N2 -i /dev/urandom | awk -v r=$range -v m=$min '{print $1 % r + m}')
echo "Random number between $min and $max: $random_number"
This method uses system entropy for randomness, which can be more random but is slightly more complex.
Using shuf command
bash
#!/bin/bash
min=5
max=15
random_number=$(shuf -i ${min}-${max} -n 1)
echo "Random number between $min and $max: $random_number"
This is simpler and more readable but requires the shuf utility to be installed.

Complexity: O(1) time, O(1) space

Time Complexity

The operations are simple arithmetic and a single random number generation, so it runs in constant time.

Space Complexity

Only a few variables are used, so the space used is constant.

Which Approach is Fastest?

Using Bash's built-in RANDOM is fastest and simplest; alternatives like /dev/urandom or shuf add overhead but may improve randomness.

ApproachTimeSpaceBest For
Bash RANDOM with moduloO(1)O(1)Simple scripts, fast execution
/dev/urandom with odO(1)O(1)Better randomness, more complex
shuf commandO(1)O(1)Readability, requires external tool
💡
Always add the minimum value after modulo to correctly shift the random number into your desired range.
⚠️
Forgetting to add 1 to the range size causes the maximum number to never be generated.