From c8ab406749124d2e762ad5cf53963070113afd0f Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 5 Apr 2017 20:06:39 +1200 Subject: Apply runtime shebanging to POSIX shell --- games/rndn.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 games/rndn.sh (limited to 'games/rndn.sh') diff --git a/games/rndn.sh b/games/rndn.sh new file mode 100644 index 00000000..18c34218 --- /dev/null +++ b/games/rndn.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# Esoteric random number generator + +# Single optional argument is a random seed, otherwise use rnds(1df) +s=${1:-"$(rnds)"} + +# Validate s +case $s in + *[!0-9]*) + printf >&2 'rndn: Seed must be non-negative integer\n' + exit 2 + ;; +esac + +# Helper functions +t() { + printf %u "$1" | cut -c -"$2" +} +l() { + printf %u "$1" | wc -c +} +c() { + printf %u "$1" | cut -c "$2" +} + +# Apply algorithm; you are not expected to understand this +s=$(t "$((s + 10))" 32) i=1 t=0 +while [ "$i" -le "$(l "$s")" ] ; do + d=$(c "$s" "$i") + t=$((t + d)) i=$((i + 1)) +done +p=$((s - t)) +while [ "$(l "$p")" -gt 1 ] ; do + j=1 q=0 + while [ "$j" -le "$(l "$p")" ] ; do + d=$(c "$p" "$j") + q=$((q + d)) j=$((j + 1)) + done + p=$q +done + +# Print result +printf '%u\n' "$p" -- cgit v1.2.3