Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Guide to Generate Random Numbers in Linux
In Linux, you can generate random numbers using several built-in methods and special files. Linux provides both pseudorandom and cryptographically secure random number generation through various commands and system interfaces. Understanding the differences between these methods helps you choose the right approach for your specific use case.
$RANDOM in Bash
The $RANDOM variable is a built-in Bash variable that generates pseudorandom numbers between 0 and 32767. This is the simplest method for basic random number generation in shell scripts.
Basic Usage
# Generate random number between 0-32767 echo $RANDOM # Generate random number in specific range (1-100) echo $((RANDOM % 100 + 1)) # Store in variable rand_num=$RANDOM echo "Random number: $rand_num"
Range Calculation Formula
# Formula: $((RANDOM % range + min)) # For range 10-20: echo $((RANDOM % 11 + 10)) # For range 1-6 (dice roll): echo $((RANDOM % 6 + 1))
Using /dev/random and /dev/urandom
Linux provides two special device files that generate random data using kernel entropy. These are more suitable for security-sensitive applications than $RANDOM.
| Device | Security | Blocking | Use Case |
|---|---|---|---|
| /dev/random | Cryptographically secure | Yes (when entropy low) | Cryptographic keys, passwords |
| /dev/urandom | Cryptographically secure | No | General applications, simulations |
Generating Numbers from Device Files
# Generate 4-digit random number
cat /dev/urandom | tr -dc '0-9' | head -c 4
echo
# Generate random hex string (8 characters)
cat /dev/urandom | tr -dc 'a-f0-9' | head -c 8
echo
# Generate random number between 1-100
od -An -N4 -tu4 /dev/urandom | awk '{print ($1 % 100) + 1}'
Using OpenSSL
OpenSSL provides cryptographically secure random number generation with various output formats.
# Generate random base64 string (6 bytes)
openssl rand -base64 6
# Generate random hex string (8 bytes)
openssl rand -hex 8
# Generate random number (using hex output)
openssl rand -hex 4 | awk '{print strtonum("0x" $0) % 1000 + 1}'
Using AWK
AWK provides the rand() function for generating pseudorandom numbers between 0 and 1.
# Generate random decimal between 0-1
awk 'BEGIN {print rand()}'
# Generate random integer between 1-10
awk 'BEGIN {print int(rand() * 10) + 1}'
# Seed with current time for better randomness
awk 'BEGIN {srand(); print int(rand() * 100) + 1}'
# Generate multiple random numbers
awk 'BEGIN {srand(); for(i=1; i<=5; i++) print int(rand() * 20) + 1}'
Comparison of Methods
| Method | Range | Security | Speed | Best For |
|---|---|---|---|---|
| $RANDOM | 0-32767 | Low | Fast | Scripts, basic tasks |
| /dev/urandom | Unlimited | High | Medium | General applications |
| /dev/random | Unlimited | Highest | Slow | Cryptographic keys |
| OpenSSL | Configurable | High | Medium | Secure applications |
| AWK rand() | 0-1 (scalable) | Low | Fast | Text processing, calculations |
Pseudorandom vs True Random
Pseudorandom Number Generators (PRNGs) use mathematical algorithms with seed values to produce sequences that appear random but are deterministic. Methods like $RANDOM and rand() are PRNGs they're fast and suitable for simulations, testing, and non-security applications.
Cryptographically secure random numbers from /dev/urandom, /dev/random, and OpenSSL use system entropy sources like mouse movements, keyboard timings, and hardware noise. These are essential for generating passwords, encryption keys, and security tokens.
Conclusion
Linux offers multiple methods for random number generation, from simple pseudorandom generators like $RANDOM for basic scripting to cryptographically secure sources like /dev/urandom for security applications. Choose /dev/urandom or OpenSSL for any security-sensitive tasks, and use simpler methods like $RANDOM for general scripting and non-critical applications.
