Bash:
Generating random numbers
How to:
In Bash, the $RANDOM
variable is the go-to for generating random numbers. Each time you reference it, Bash provides a pseudorandom integer between 0 and 32767. Let’s explore some practical examples:
# Basic usage of $RANDOM
echo $RANDOM
# Generating a random number in a specified range (0-99 here)
echo $(( RANDOM % 100 ))
# Generating a more "secure" random number, suitable for passwords or keys
# Using /dev/urandom with od command
head -c 8 /dev/urandom | od -An -tu4
# Seeding RANDOM for reproducibility
RANDOM=42; echo $RANDOM
Sample output (note: actual output will vary since the numbers are random):
16253
83
3581760565
17220
Deep Dive
The mechanism behind Bash’s $RANDOM
generates pseudorandom numbers, meaning they follow an algorithm and can, in theory, be predictable - a potential security flaw for applications requiring genuine unpredictability. Modern cryptographic applications usually require randomness derived from physical phenomena or from hardware designed specifically to generate random data, such as /dev/urandom
or /dev/random
in Linux, which gather environmental noise.
For casual or non-security critical tasks, $RANDOM
suffices and offers the benefit of simplicity. However, for cryptographic purposes or where randomness quality is critical, developers should look towards other tools and languages designed with cryptography in mind, such as OpenSSL or programming languages with robust random number generator libraries.
While Bash’s $RANDOM
serves its purpose in scripts requiring basic random numbers, its limitations should steer developers towards more robust solutions for applications where the quality or security of the randomness matters.