A 140byt.es gist to generate a simulated dice pool.
-
-
Save BenOvermyer/1154204 to your computer and use it in GitHub Desktop.
function(n,s,x,v){ // variable n is the number of dice to roll, s is number of sides, x is success value, v is number of successes to start with | |
for(i=0;i<n;i++){ // begin rolling loop | |
r=0|(Math.random()*s)%s+1; // "roll" a single die | |
if(r>=x){ // compare the result to the success value | |
v++; // if a success, increment the counter | |
} | |
} | |
return v; // output the number of successes | |
} |
function(n,s,x,v){for(i=0;i<n;i++){r=0|(Math.random()*s)%s+1;if(r>=x){v++;}}return v;} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Ben Overmyer <www.manatrance.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "dicePool", | |
"description": "Generate a simulated dice pool.", | |
"keywords": [ | |
"dice", | |
"random", | |
"rpg" | |
] | |
} |
<!DOCTYPE html> | |
<title>Dice Pool Roller</title> | |
<script> | |
var diceRoll = function(n,s,x,v){for(i=0;i<n;i++){var r=0|(Math.random()*s)%s+1;if(r>=x){v++;}}return v;} | |
console.log(diceRoll(12,6,4,0)); | |
console.log(diceRoll(18,6,4,0)); | |
</script> |
Feel free to take it. If you have any question, feel free to ask - we're a pretty helpful bunch here ;-)
At least combine the var-statements: var x=1,y=0,...;
The Byte-Saving Techniques https://github.com/jed/140bytes/wiki/Byte-saving-techniques would help.
@atk - I'm curious, how does this part of your version work?
v+=(1+(0|Math.random()*s))>x;
There's no IF in there anywhere.... some kind of shorthand I'm missing?
The answer is: Coercion. Let us dissect the corresponding part:
1+(0|Math.random()*s)
generates a floored Number between 1 and s (0| coerces the float-points produced by Math.random() to integer).
...>x
compares the resulting number with x and returns true if bigger and false if smaller - at this point we have a boolean.
v+=...
adds the expression to v, coercing true to 1 and false to 0 - only if the result is bigger, v gets incremented.
Modified a bit, though not as efficiently as your variation.
...now I'm really starting to get into JS. Good way to start the morning!!!