diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-07-03 16:37:22 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-07-03 16:43:49 +1200 |
commit | 44dbf31927601ae69f343957a1046c4aea26d72d (patch) | |
tree | 37cddada6f82008e6228e514894e1377d12b1386 /games/rndn | |
parent | Apply spacing/comments (diff) | |
download | dotfiles-44dbf31927601ae69f343957a1046c4aea26d72d.tar.gz dotfiles-44dbf31927601ae69f343957a1046c4aea26d72d.zip |
Add seed-setting option and handling
Diffstat (limited to 'games/rndn')
-rwxr-xr-x | games/rndn | 35 |
1 files changed, 34 insertions, 1 deletions
@@ -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 |