aboutsummaryrefslogtreecommitdiff
path: root/games/rndn
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-07-03 16:37:22 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-07-03 16:43:49 +1200
commit44dbf31927601ae69f343957a1046c4aea26d72d (patch)
tree37cddada6f82008e6228e514894e1377d12b1386 /games/rndn
parentApply spacing/comments (diff)
downloaddotfiles-44dbf31927601ae69f343957a1046c4aea26d72d.tar.gz
dotfiles-44dbf31927601ae69f343957a1046c4aea26d72d.zip
Add seed-setting option and handling
Diffstat (limited to 'games/rndn')
-rwxr-xr-xgames/rndn35
1 files changed, 34 insertions, 1 deletions
diff --git a/games/rndn b/games/rndn
index b173dce6..56a19be3 100755
--- a/games/rndn
+++ b/games/rndn
@@ -2,9 +2,42 @@
# Esoteric random number generator
# <http://dilbert.com/strip/2001-10-25>
+self=rndn
+
+# Define usage function
+usage() {
+ printf 'USAGE: %s [-h | -s SEED]\n' "$self"
+}
+
+# Parse options
+while getopts 'hs:' opt ; do
+ case $opt in
+
+ # -h for help
+ h)
+ usage
+ exit 0
+ ;;
+
+ # -s to set seed manually
+ s)
+ seed=$OPTARG
+ ;;
+
+ # Unknown option, print usage and fail
+ \?)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# If no seed given, get one from Bash's $RANDOM
+: "${seed:=$RANDOM}"
# Apply algorithm
-for ((seed = RANDOM ** 2, i = 0; i < ${#seed}; i++)) ; do
+for ((i = 0; i < ${#seed}; i++)) ; do
((sum += ${seed:i:1}))
done
for ((red = seed - sum; ${#red} > 1; red = redn)) ; do