Contents

Home

Contact

Random Map Generators
Other Generators
GIMP Scripts
Gaming resources
Miscellany

Finding which dice to roll for a given range

Sometimes, particularly in classic Dungeons & Dragons, we are asked to roll a result within a given range where the particular dice to be used are not specified.

At its simplest, the solution is of the form XdY (a roll of X Y-sided dice). In this case, if the range has a minimum value of A and a maximum value of B, then X = A and Y = B/A. So, for example, if we want to roll 40-400, we have A = 40, B = 400, and hence X = 40 and Y = 10: the solution is 40d10.

This isn't the only kind of solution, however. What happens if we want to allow rolls to have an optional modifier added? Or if we want to additionally allow multiple different kinds of dice to be rolled?

The easiest approach is to brute-force the solutions -- to try every possible combination of dice, numbers of dice, and modifiers. In the general case, however, where we might be using several different kinds of die, the algorithm can take a very long time. Fortunately, we can improve slightly upon the brute-force method by using what we know about the dice and the range to reduce the number of variables which must be iterated.

Part 1: Solutions using only one type of die and a possible modifier

To roll a minimum of A and a maximum of B with XdY+M, we require:

X + M = A       (1)
XY + M = B       (2)

Here we have two equations in three unknowns so we will be able to solve for two values, leaving the third as a parameter. We will solve for X, the number of dice, and M, the modifier, leaving the number of sides of the dice, Y, unknown.

First solve for M in Equation 1:

M = A - X

Then substitute M into Equation 2 and solve for X:

XY + A - X = B

X = (B - A) / (Y - 1)

Now M depends on X, which in turn depends on Y -- hence any solution depends entirely on Y, the number of sides.

Note that in order for a solution to be valid, X must be an integer (we cannot roll a fractional number of dice). So we can find solutions for any range A-B by testing each allowed value of Y (4, 6, 8, 10, 12, 20); when X = (B - A) / (Y - 1) is an integer, we know that Y is a valid die, and we can then compute M = A - X to find the modifier.

Example: 3-18.

Here A = 3 and B = 18, so B - A = 15.

First test the d4. If Y = 4, then X = (B - A) / (Y - 1) = 15 / 3 = 5. Since 5 is an integer, we have a solution involving 5d4. The modifier is M = A - X = 3 - 5 = -2. The d4 solution is therefore 5d4-2.

Next test the d6. If Y = 6, then X = (B - A) / (Y - 1) = 15 / 5 = 3. Since 3 is an integer, we have a solution involving 3d6. The modifier is M = A - X = 3 - 3 = 0. The d6 solution is therefore simply 3d6.

Next test the d8. If Y = 8, then X = (B - A) / (Y - 1) = 15 / 7. This is not an integer so there is no solution involving the d8.

Any remaining solutions will be found by testing the rest of the dice in turn.

Part 2: Solutions using multiple types of die and a modifier.

To roll a minimum of A and a maximum of B with some number of d4, d6, d8, d10, d12, d20, and a modifier, we require that:

4 X1 + 6 X2 + 8 X3 + 10 X4 + 12 X5 + 20 X6 + X7 = B       (1)
X1 + X2 + X3 + X4 + X5 + X6 + X7 = A       (2)

where X1 is the number of d4, X2 is the number of d6, etc.

Here we have a system of two equations in seven unknowns so again we will be able to solve for two values, but in this case we will be left with five free parameters which must be varied.

I used a computer algebra package (Maxima) to solve the above system for X7, the modifier, and X6, the number of d20:

X7 = -(8 X5 + 10 X4 + 12 X3 + 14 X2 + 16 X1 - 20 * A + B) / 19
X6 = -(11 X5 + 9 X4 + 7 X3 + 5 X2 + 3 X1 + A - B) / 19.

Now we just need to iterate over all permissible values of X1, X2, X3, X4, and X5 -- i.e., the numbers of d4, d6, d8, d10, and d12 -- such that Equations 1 and 2 hold after substituting those values into the equations for X6 and X7.

Programs

An online calculator for Part 1

Part 1 implemented in C

Part 2 implemented in C