Frequently Asked Question
What is $RANDOM and is it good enough for anything serious?
$RANDOM is a magic bash variable: every time you read it, it yields a fresh
pseudo-random integer in the range 0–32767. For example, echo $((RANDOM % 100))
prints a number from 0 to 99, useful for picking a random sleep interval, jittering
a retry, or making a temp filename unique alongside the PID ($$). It is built into
the shell, requires no external tool, and works in any modern bash.
It is not suitable for anything where the random number affects security. The
underlying generator is a linear-congruential PRNG seeded from the shell's PID and the
current time, well-understood and trivially predictable from a few outputs. For
cryptographic purposes, generating tokens, passwords, keys, secrets, read from
/dev/urandom instead, for example with head -c 16 /dev/urandom | base64 or
openssl rand -hex 16. For shuffling lines randomly use shuf; for a uniform integer
in an arbitrary range, shuf -i 0-99 -n 1 is cleaner than the modulo trick (which has
a tiny bias when the range does not divide 32768 evenly).