Dr Adam Stanton

Researcher in Adaptive Informatics

How can I make D&D more nerdy?

Here’s a bash function for rolling the dice!

d() { if echo $1 | egrep -q "[0-9]*d[0-9]+($|(?:(?:\+|\-){1}[0-9]+$){1})"; then eval $( echo $1 | awk -F'[d+-]' '{print "n="$1";s="$2";m="$3}' ); t=0; for (( c=1; c<=$n; c++ )) ; do v=$((1+RANDOM%$s));echo $v; let t=t+v; done; if [ -z ${m} ]; then echo "Total: $t"; else if echo $1 | egrep -q "\+"; then echo "Total: $t+$m=$((t+m))"; else echo "Total: $t-$m=$((t-m))"; fi; fi; else echo "Incorrect dice code format.";echo; fi }

Here's an example use:
mbp:~ user$ d 1d20; d 4d8+4
19
Total: 19
2
1
4
6
Total: 13+4=17

Comments are closed.