Random Number

Kevin FOO
1 min readSep 20, 2024

--

Generating random number in shell

$RANDOM

echo $RANDOM

This isn’t a good random number generator as it might generate the same number if badly written.

# Generate a DIFFERENT random number between 10-99 everytime
r=$RANDOM$RANDOM; echo $r | cut -c1-2

# Generate SAME number between 10-99 everytime
echo $RANDOM$RANDOM | cut -c 1-2

# cut -c 1-2 : Get character 1 to 2

shuf

# Generate a random number between 10-99
shuf -i 10-99 -n 1

# -i 10-99 : Generate number ranged between 10-99
# -n 1 : Generate once

It might not be installed in macOS and most probably comes preinstalled in Ubuntu.

# macOS
brew install coreutils

od (octal dump)

# Generate a random number between 10-99
od -An -N4 -tu4 /dev/urandom | tr -d ' ' | cut -c 1-2

# -An : Suppresses the file offset output
# -N4 : Reads 4 bytes
# -tu4 : Interprets the bytes as unsigned decimal integers
# tr -d ' ' : Removes any spaces from the output
# cut -c 1-2 : Get character 1 to 2

< Back to all the stories I had written

--

--

Kevin FOO
Kevin FOO

Written by Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

No responses yet