Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Created May 11, 2017 09:18
Show Gist options
  • Save rawiriblundell/b9d4faafb1c31264ae5181897c7f0cdb to your computer and use it in GitHub Desktop.
Save rawiriblundell/b9d4faafb1c31264ae5181897c7f0cdb to your computer and use it in GitHub Desktop.
Linear congruential generator (POSIX-ish shell)
#! /bin/bash
# If the special variable isn't available (e.g. dash shell), we fall back to a BSD-style
# Linear congruential generator (LCG) which we use to create our own '$RANDOM' variable
# See: https://rosettacode.org/wiki/Linear_congruential_generator
# If two invocations of RANDOM are the same (or both are blank),
# then we're working with a shell that does not support $RANDOM.
if [ "${RANDOM}" = "${RANDOM}" ]; then
# We set the initial seed for the LCG
# First we check if /dev/urandom is available.
if [ -c /dev/urandom ] && command -v od >/dev/null 2>&1; then
# Get a string of bytes from /dev/urandom using od
rnSeed=$(od -N 4 -A n -t uL /dev/urandom | tr -d " ")
# Otherwise we can just seed it using the epoch
else
rnSeed=$(date +%s)
fi
# BSD style LCG modulus'ed against 2^31
rnSeed=$(( (1103515245 * rnSeed + 12345) % 2147483648 ))
# Print as an unsigned integer, divided by 2^16
RANDOM="$(printf "%u\n" "$(( rnSeed / 65536 ))")"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment