Created
September 30, 2012 20:32
-
-
Save robbieferrero/3808367 to your computer and use it in GitHub Desktop.
Dice notation roller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dice = function(d) { | |
var a = d.split('d'), | |
result = 0, | |
num = (typeof (a[0]-0) === 'number') ? a[0] : 1, | |
op = (a[1].match(/[\-\+]/)), | |
max = (op === null) ? a[1] : a[1].split(op[0])[0], | |
mod = (op !== null) ? op[0] + ' ' + a[1].split(op[0])[1] : ''; | |
for (var i = num; i >= 0; i--) { | |
result += (Math.floor(Math.random() * (max)) + 1) | |
} | |
return new Function('return ' + result + mod)(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment