Something Neat About Online Dice

I just realized something interesting about rolling dice online. Forgive my geek side for showing for a moment.

I've been messing with modules for JibbyBot (a phenny bot for IRC), trying to pick up a little Python here and there, and something I thought to possibly implement is a dice-rolling module that would let you specify die-rolls in D&D format. I suppose a brief tutorial is in order.

In Dungeons and Dragons (D&D), there are several different polyhedral dice. When the game wants you to roll a specific die, it will specify the number of sides on the die to roll. So for the frequent opportunity you get to roll a 20-sided die, the game will specify this with a shorthand syntax, "d20", which is really short for "1d20", meaning to roll the 20-sided die one time. If the game needs you to roll a 6-sided die three times, the shorthand would look like "3d6". There are also things that let you add or subtract a constant value from the roll, like in "3d6+2", but that's not quite relevant for the interesting thought I have come across.

3d6 is a pretty standard roll in D&D. It (or a very similar variation) is used when you create a new character for each of the six stats that represent that character's abilities. This roll has a certain mathematical property in that the range of numbers is between 3 (the total of dice all rolling 1) and 18 (the total of three dice all rolling 6). When producing characters in the past via automated random number generators, I would simply generate a random number between 3 and 18 and call it a day. As it turns out, this is not an accurate representation of 3d6.

The tricky bit is that 3d6 will result in a more interesting pattern over time. If you roll a lot of 3d6 results with real dice, you'll note that there is a bell-shaped curve to the results. The numbers 10 and 11 are far more common than 3 or 18. This is because there is only one way to roll the dice and get 1-1-1 for a total of 3, but there are three ways to roll the dice to get 4 (1-1-2, 1-2-1, and 2-1-1). The numbers 10 and 11 have the most possible combinations of values, so they appear more often over time.

So if you're creating a random number generator that simulates rolling dice, you need to factor in that probability to your results, or you're going to be very disappointed. 1d15+3 will definitely not yield the same results as 3d6 over many successive rolls.

I'm sure this math is pretty obvious, but it's something that I had not thought about before, and yet was curious about it when I saw that there are die rollers that still go through iterations of rolls to simulate this curve rather than just picking a flat number at random from within the range. Interesting. I wonder if there's an easy mathematical way to simulate the curve without doing iterations.


2 Responses to Something Neat About Online Dice

  1. Greg from canadianrules.ca 1970-01-01T00:00:00+00:00

    Sure, you have to have the possibilities enumerated, then randomly pick one of the possibilities. (ie, {1,1,1} {1,1,2} {1,2,1} etc.)

    It's probably more intuitive to just iterate though, since you'd have to iterate to create the list of possibilities (unless you store them)

  2. owen from asymptomatic.net 1970-01-01T00:00:00+00:00

    Just poking at this problem again, it comes down to a matter of using different number bases.

    3d6 has 6^3 (216) different combinations of outcomes. So the key is in using base 6. To get a random dice roll of 3d6 using that distribution without having to enumerate all the combinations, first pick a random number from 0 to 215 in the total possible range of combinations. Convert that number to base 6. Ignoring the base, add one to each digit, since each die ranges from 1 to 6, not from 0 to 5. Sum the individual digits.

    For example, pick the random number of 155. 155 in base 6 is 415. Add one to each digit to get 5-2-6. Sum the digits to get a total of 13.

    This should still work for larger-sided dice, provided that you have a single digit for each base and sum them individually in base 10. So 2d20 would have 20^2 combinations, and a random pick of 383 would lead to a base-20 number of [19][3]. Add one to each digit and get a total of 24.

2682