Getting started with the $RANDOM Variable in Bash

Published: 2022 February 07

system administration Linux Bash shell command

Creating Randomness in Bash

There are many shell variables used in Bash. One of them is the Bash variable $RANDOM.

$RANDOM returns a pseudo-random number between 0 and 32767.

Try it by typing the following command into a Bash command line interpreter:

echo $RANDOM

Run the command multiple times to see different results.

Changing the possible return range

We can reduce the range of the output by using the modulo operator.

echo $((RANDOM % 2))

This will return numbers in the range 0-1.

echo $((RANDOM % 3))

This will return numbers in the range 0-2.

echo $((RANDOM % 10))

This will return numbers in the range 0-9.

Changing the possible return minimum value

We can increase the minimum value of the possible return range by adding a number to the remainder.

echo $((RANDOM % 3 + 1))

This will return numbers in the range 1-3.

echo $((RANDOM % 3 + 2))

This will return numbers in the range 2-4.

echo $((RANDOM % 3 + 10))

This will return numbers in the range 10-12.

Selecting the possible return range

Using the above examples, you can see that the range can be thought of as a simple formula: $RANDOM % Y + X, where X is the minimum value of the range, and Y is the number of values in the range.

So, if you want a range going from 100 to 110, the minimum is 100 (X=100) and includes 11 numbers (Y=11):

echo $((RANDOM % 11 + 100))

Or, a range of 200 to 250, starts at 200 (X=200) and includes 51 numbers (Y=51):

echo $((RANDOM % 51 + 200))

Or, a range of 5000 to 6000, begins with 5000 (X=5000) and includes 1001 numbers (Y=1001):

echo $((RANDOM % 1001 + 5000))

A note about bias in this approach

As this StackOverflow answer points out, the results of the above techniques will “be very slightly biased for any divisor that doesn’t divide equally into 32768 (eg, anything that isn’t a power of two)”.

If this matters for your use case you can address this using a slightly different approach.

As another StackOverflow answer states, “To convert this number to an integer in the range 0..2, you can multiply it by 3 and (integer) divide by 32768 (=32767+1).”

So, one approach is to use the formula: $RANDOM * Y / 32768 + X, where X is the minimum value of the range, and Y is the number of values in the range.

If you want a range going from 100 to 110, the minimum is 100 (X=100) and includes 11 numbers (Y=11):

echo $((RANDOM * 11 / 32768 + 100))

Or, a range of 200 to 250, starts at 200 (X=200) and includes 51 numbers (Y=51):

echo $((RANDOM * 51 / 32768 + 200))

Or, a range of 5000 to 6000, begins with 5000 (X=5000) and includes 1001 numbers (Y=1001):

echo $((RANDOM * 1001 / 32768 + 5000))

Copying Directory Structure With a Bash Script - With Help From ChatGPT AI

Published: 2024 January 22

Setting Up a Raspberry Pi as a Linux Print Server with CUPS

Published: 2024 January 19

Setting Up a Raspberry Pi Zero 2 W with Ubuntu Server

Published: 2024 January 04