Created — forked from 140bytes/LICENSE.txt

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Dice Pool 140

View LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
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.
View LICENSE.txt

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

View LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11 12
function(n){ // variable n is the number of dice to roll
var d=6; // six sides
var x=4; // must get a four or higher to be a success
var v=0; // success counter
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
}
View LICENSE.txt
1
function(n){var d=6;var x=4;s=6;x=4;v=0;for(i=0;i<n;i++){r=0|(Math.random()*s)%s+1;if(r>=x){v++;}}return v;}
View LICENSE.txt
1 2 3 4 5 6 7 8 9 10 11
{
"name": "dicePool",
 
"description": "Generate a simulated dice pool.",
 
"keywords": [
"dice",
"random",
"rpg"
]
}
View LICENSE.txt
1 2 3 4 5 6 7
<!DOCTYPE html>
<title>Dice Pool Roller</title>
<script>
var diceRoll = function(n){var d=6;var x=4;s=6;x=4;v=0;for(i=0;i<n;i++){r=0|(Math.random()*s)%s+1;if(r>=x){v++;}}return v;}
console.log(diceRoll(12));
console.log(diceRoll(18));
</script>
atk commented

Even if it is not 140bytes yet, please show us your code so we could help you golfing it down to the desired size.

There are many similar forks these day...

Owner

I'm brand new to this. If a dice pool has been done before, I'd rather do something new and interesting.

atk commented

tsaniel referred to forks without actual script content; as far as I know a dice pool has not yet been done as 140byt.es gist. How would you implement a dice pool in JS anyway? Would you just input the number and faces of the dices and add Math.random()*faces every time? Or would you support complex game mechanics described by a string parsed with a regex, e.g. "3*d6+d20-d8"?

Owner

This particular dice roller is just intended for d6-based wargames; it'd be fairly easy to modify it to do other die types. Complex dice rolls are a different matter.... might do that for the next gist.

atk commented

Just tried my hand at the complex roll but without target check:

function(a,b){return b=0,a.replace(/(-?)((\d+)\*|)d(\d+)/g,function(c,d,e,f,g){for(f=f||1;f--;b+=(d+1)*(1+(0|Math.random()*g)));}),b}

Be sure not to leave your variables in the scope - either use var or argument variables. Try to optimize your version according to the tips in the Wiki, e.g. Math.round can be replaced with 0| - and you probably wanted Math.floor anyway. There are many places to exploit coercions on this one, too.

Other than that, the function can be executed on the console, no need for prompt or alert, rather use variables and return, like this:

function(n,s,x,v,i){for(v=i=0;i<n;i++)v+=(1+(0|Math.random()*s))>x;return v}
Owner

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!!!

atk commented

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

atk commented

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

Owner

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

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
Something went wrong with that request. Please try again.