Skip to content

Instantly share code, notes, and snippets.

@BenOvermyer
Forked from 140bytes/LICENSE.txt
Created August 18, 2011 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenOvermyer/1154204 to your computer and use it in GitHub Desktop.
Save BenOvermyer/1154204 to your computer and use it in GitHub Desktop.
Dice Pool 140

A 140byt.es gist to generate a simulated dice pool.

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>
@atk
Copy link

atk commented Aug 19, 2011

Feel free to take it. If you have any question, feel free to ask - we're a pretty helpful bunch here ;-)

@atk
Copy link

atk commented Aug 19, 2011

At least combine the var-statements: var x=1,y=0,...;

@tsaniel
Copy link

tsaniel commented Aug 20, 2011

The Byte-Saving Techniques https://github.com/jed/140bytes/wiki/Byte-saving-techniques would help.

@BenOvermyer
Copy link
Author

@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?

@atk
Copy link

atk commented Aug 24, 2011

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