Skip to content

Instantly share code, notes, and snippets.

@reinh
Created July 19, 2019 23:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinh/98dda5ea821d958c9b3921b2de353c25 to your computer and use it in GitHub Desktop.
Save reinh/98dda5ea821d958c9b3921b2de353c25 to your computer and use it in GitHub Desktop.
# roll 2d6+3
function roll {
local match total
local -a rolls
if ! [[ "$1" =~ '([0-9]+)d([0-9]+)(\+[0-9]+)?' ]]; then
echo "usage: roll 2d6+3" 1>&2
return 1
fi
for ((n=0;n<$match[1];n++)); do
((total += m = $[$RANDOM % $match[2] + 1]))
rolls+=($m)
done
((total += match[3]))
local IFS="+"
echo "\u001b[1m$total\u001b[0m (${rolls[*]}$match[3])"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment