# JS Dice Roller

This topic is 1190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Just wondering if someone can help me with this JS Dice roller. For 5d100 I'm getting numbers like 1400.

this.roll = function(amount, sides, mod = 0)
{

var min = amount;
var max = sides * amount;
var value = 0;

for (i = 0; i < amount; i++)
{
value = value + (Math.floor(Math.random() * (max - min + 1)) + min);
}

value = value + mod;

return value;
// end of roll
}


##### Share on other sites

never programmed JS, but 'min' would be 5 and 'max' would be 500, I guess, and then you're doing 5 rolls between 5 and 500, by the looks of it.

max-min = 500 - 5 is about 500, 500 * random is about 250, 250 * 5 times (for-loop) is 1250 on average.

Edit: Either do one draw between min and max, or do 5 draws but don't add 'amount' into min and max then.

Edited by Alberth

##### Share on other sites

That's weird. This function returns a single random number in a range.

	this.range = function(min, max, mod = 0)
{

return (Math.floor(Math.random() * (max - min + 1)) + min) + mod;

// end of range
}


I figured to get a die roller you would do the amount of sides times that range, while adjusting minimum to be the amount of dice.

Edited by JeremyB

##### Share on other sites

So this is right

	this.roll = function(amount, sides, mod = 0)
{

var min = amount;
var max = sides;
var value = 0;

for (i = 0; i <= amount; i++)
{
value = value + (Math.floor(Math.random() * (max - min + 1)) + min);
}

value = value + mod;

return value;
// end of roll
}


##### Share on other sites

The 'min' and 'max' is about the value of a single roll.

They are not related to how many rolls you do.

It's like your money coins. They have a (sort of) random value, but their value is unrelated to how many coins you have. (Would be great though, oh man, I would be so much collecting coins then ).

"amount" is the number of rolls, which thus should not be assigned to "min". I guess it's 1 or some other lowest value of your die.

There is a simple way to test these things. Instead of using "Math.random()", try 0 (ie lowest value for all rolls) and 1 0.99999 (ie highest value for all rolls).

Then try a few cases, like 1d6, 5d100,  and 100d1.

You can even run the computation manually, just compute the values yourself. You can also use a debugger or add "print" statements to check how much you add each iteration.

If you do this, you will quickly see what value is wrong.

EDIT: You need to use 0.999999  instead of 1  for highest value of a roll  see also http://www.w3schools.com/jsref/jsref_random.asp

Hmm, I continue editing this post :)

To show you what I mean by the number replacement, see the code below

def unlucky(amount, sides, mod = 0):
min = amount;
max = sides;
value = 0;

for i in range(amount + 1): # for (i = 0; i <= amount; i++)
value = value + int(0.0 * (max - min + 1)) + min;

value = value + mod;

return value;

def lucky(amount, sides, mod = 0):
min = amount;
max = sides;
value = 0;

for i in range(amount + 1): # for (i = 0; i <= amount; i++)
value = value + int(0.99999 * (max - min + 1)) + min;

value = value + mod;

return value;

print("1d6   min={}, max={}".format(unlucky(1, 6), lucky(1, 6)))
print("2d6   min={}, max={}".format(unlucky(2, 6), lucky(2, 6)))
print("100d1 min={}, max={}".format(unlucky(100, 1), lucky(100, 1)))

I copied your roll_dice function, into 'unlucky', converted to Python (I don't have Javascript running) and replaced "Math.random()" by "0.0"

I did the same for "lucky", but used 0.999999 instead of 0.0.

Then I added a few test cases, to check results.

The problem with random is that you don't know what it returns, which makes it very difficult to check whether the code is correct. By the above change, you have fixed return values (namely "lowest possible" and "highest possible"), which makes checking much easier.

Once you are satisfied with the results, throw away one of the functions, and replace the constant again with Math.random(), and you're done.

As for tests: My results are

1d6   min=2, max=12
2d6   min=6, max=18
100d1 min=10100, max=303


so it seems it's still wrong.

The first result suggests you are throwing one die to many. (And indeed, 0 to .. <= 1 is 2 rolls rather than 1, I see now.)

Edited by Alberth

##### Share on other sites

I always use module for this operation:

result = 0;
for number of rolls
result += 1 + (random() % sides);



##### Share on other sites

@arka80: That would fail, as Javascript uses [0, 1) as result of the "random" call, see also my link.

##### Share on other sites

EDIT: You need to use 0.999999  instead of 1  for highest value of a roll  see also http://www.w3schools.com/jsref/jsref_random.asp

Why would you use 0.999999? I don't think that's right.

##### Share on other sites

Why would you use 0.999999? I don't think that's right.
That link says "Return a random number between 0 (inclusive) and 1 (exclusive):"

The "exclusive" means 1.0 is NOT included, so technically you need 1-(smallest representable value such that you end up just below 1).

I would say 0.000001 is a pretty good approximation for that small value, until you have a die with about 1,000,000 sides.

##### Share on other sites

Amount a min should be the same thing. There is 1 side for each dice. So if the first argument is 2 then min is 2, one for each side, and there are 2 rolls taking place. It's still a range between min max.. it's just min is also amount...

Right? Them loop for amount of rolls as well.. and add the mod.

I can't see where the code below would be wrong.

this.roll = function(amount, sides, mod = 0)
{

var min = amount;
var max = sides;
var value = 0;

for (i = 0; i <= amount; i++)
{
value = value + (Math.floor(Math.random() * (max - min + 1)) + min);
}

value = value + mod;

return value;
// end of roll
}


You're right though. It's giving constant 2 for 1d1.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015434
×