▶ Random Everything - 100+ Free Generators // v2.5.0
root@generate-random:~/random-number-bash$ _

Bash Random Number Generator

Random numbers in Bash: a random integer in any range, floats, picking and shuffling, and secure random values - with ready-to-paste code

How to generate a random number in Bash: $RANDOM for quick scripts, shuf or /dev/urandom for wider ranges and anything security-related. Choose a range above for a ready-to-paste line.

Random Number in a Range

$RANDOM returns 0 to 32767 in Bash (not in plain sh). The modulo trick is slightly biased and limited to 32768 values; shuf has neither problem.

echo $(( RANDOM % 10 + 1 ))   # 1..10, quick and slightly biased
shuf -i 1-10 -n 1              # 1..10
shuf -i 1-1000000 -n 5         # five different numbers

Random Line from a File

shuf also picks lines.

shuf -n 1 names.txt     # one random line
shuf names.txt          # whole file in random order

Secure Random Numbers and Strings

Read from the kernel CSPRNG for anything security-related.

od -An -N4 -tu4 /dev/urandom                  # random 32-bit unsigned integer
shuf -i 1-10 -n 1 --random-source=/dev/urandom
openssl rand -hex 16                          # 32 hex characters
head -c 16 /dev/urandom | base64

Common Mistakes

$RANDOM does not exist in /bin/sh on systems where sh is dash (Debian, Ubuntu) - run the script with bash. On macOS, shuf comes from GNU coreutils (installed as gshuf).

Frequently Asked Questions

How do I generate a random number between 1 and 10 in Bash? ▶
shuf -i 1-10 -n 1, or echo $(( RANDOM % 10 + 1 )) for a quick, slightly biased value.
What is the range of $RANDOM? ▶
0 to 32767.
How do I generate a secure random number in Bash? ▶
Read from /dev/urandom, for example od -An -N4 -tu4 /dev/urandom, or use shuf with --random-source=/dev/urandom.
How do I generate a random string in Bash? ▶
openssl rand -hex 16, or head -c 16 /dev/urandom | base64.

[ HOW TO CITE THIS PAGE ]

APA Style:
Generate-Random.org. (2026). Bash Random Number Generator. Retrieved from https://generate-random.org/random-number-bash
Web Citation:
Bash Random Number Generator - Generate-Random.org (https://generate-random.org/random-number-bash)