A 140byt.es gist to generate a simulated dice pool.
-
-
Save BenOvermyer/1154204 to your computer and use it in GitHub Desktop.
Dice Pool 140
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
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 | |
} |
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
function(n,s,x,v){for(i=0;i<n;i++){r=0|(Math.random()*s)%s+1;if(r>=x){v++;}}return v;} |
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
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. |
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
{ | |
"name": "dicePool", | |
"description": "Generate a simulated dice pool.", | |
"keywords": [ | |
"dice", | |
"random", | |
"rpg" | |
] | |
} |
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
<!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> |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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?