About

The following sections describe ways, depending on what tools are available, to generate random primitives on the command line such as numbers or strings.

Primitive

We define primitives as primitive types that might be one-level up in terms of specialization; for instance, an UUID is just a hexadecimal string, but in this definition it is considered a primitive type. The same applies to MD5.

Numbers

Command Description
$((RANDOM)) a bashism: a random number in the interval $[0, 65535]$ or $[0, 32760]$
$(( RANDOM % (10 - 5 + 1 ) + 5 )) a random number in the interval $[5, 10]$ using clamping
shuf –random-source='/dev/urandom' -i 1-100 -n 1 generates one (-n 1) random number in the interval $[1, 100]$ (-i 1-100)
jot -r 1 1 100 generate one (-r 1) random number in the interval $[1, 100]$
awk -v min=5 -v max=10 'BEGIN{srand(); print int(min + rand() * (max - min + 1))}' generates a random number between $5$ and $10$ (-v min=5 -v max=10) using a random number generator seeded by time (srand()) and then clamping the random number of a range

Notes

  • Generating a random number is frequently expressed as just the way to create the actual random number seed but clamping the random number to a specific interval is usually a matter of some algebra that can be done in various ways such that it is often omitted.
  • Using /dev/random or /dev/urandom has the advantage of using a larger entropy pool provided by Linux, and, where available, a device like onerng can be used as a source of true entropy.

UUID

Command Description
cat /proc/sys/kernel/random/uuid a random UUIDv4 from the kernel
uuidgen found in package uuid-runtime on Debian and derivatives

MD5

Command Description
dd if=/dev/random status=none count=1 bs=512 | md5sum generates a random MD5 hash with $512$ bytes (bs=512) of entropy from /dev/random

Strings

Command Description
cat /dev/urandom | tr -dc 'A-Za-z0-9' | fold -w 5 | head -n 1 generate one ( head -n 1) random string of length 5 (fold -w 5) using the character class A-Za-z0-9 (-dc A-Za-z0-9)
tr -dc '[:graph:]' < /dev/urandom | head -c 10 generate $10$ characters (-c 10) of printable characters except space ([:graph:])

sh/generating_random_primitives.txt · Last modified: by office

Wizardry and Steamworks

© 2025 Wizardry and Steamworks

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.