aboutsummaryrefslogtreecommitdiff
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
parentApply spacing/comments (diff)
downloaddotfiles-44dbf31927601ae69f343957a1046c4aea26d72d.tar.gz
dotfiles-44dbf31927601ae69f343957a1046c4aea26d72d.zip
Add seed-setting option and handling
-rwxr-xr-xgames/rndn35
-rw-r--r--man/man6/rndn.69
2 files changed, 41 insertions, 3 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
diff --git a/man/man6/rndn.6 b/man/man6/rndn.6
index 11169f3c..ee7ad7a2 100644
--- a/man/man6/rndn.6
+++ b/man/man6/rndn.6
@@ -5,12 +5,17 @@
.SH USAGE
.B rndn
.br
+.B rndn -s 381290
+.br
business-critical-process -t "$(\fBrndn\fR)"
.SH DESCRIPTION
.B rndn
uses an advanced but somewhat esoteric algorithm derived by Adams (2001) to
-return a random number. While it has proven robust in the author's production
-usage, its algorithm has not been formally verified.
+return a random number. The seed can be derived internally or specified with
+the -s option.
+.P
+While rndn(6) has proven robust in the author's production usage, its algorithm
+has not been formally verified.
.SH SEE ALSO
<http://dilbert.com/strip/2001-10-25>
.SH AUTHORS