Maths โ
Combine dice rolls with mathematical operators and functions to calculate damage, apply bonuses or build more complex expressions.
When a dice roll appears in a calculation, its total is used.
Use variables to include values supplied by your application, such as a character's bonus or the number of dice to roll.
Operators โ
The following operators are supported:
| Operator | Operation | Example |
|---|---|---|
+ | Addition | 2d6+3 |
- | Subtraction | 2d6-2 |
* | Multiplication | d6*5 |
/ | Division | 2d10/d20 |
% | Modulo | d15%2 |
^ or ** | Exponentiation | 3d20^4 |
For example:
d6*5 // Multiply the result of one six-sided die by 5
2d10/d20 // Divide the total of two ten-sided dice by the result of one twenty-sided die
3d20^4 // Raise the total of three twenty-sided dice to the fourth power
3d20**4 // Equivalent to 3d20^4
d15%2 // Calculate the result modulo 2To subtract a dice roll, put a value or expression before the minus sign. 10-2d6 subtracts the total of two d6s from 10. To get the negative of the roll on its own, use 0-2d6. The forms -2d6 and -(2d6) are invalid.
Modulo uses floored division, so a non-zero result has the sign of the divisor. For example, -5%3 is 1 and 5%-3 is -1. This differs from JavaScript's remainder operator. Modulo by zero or with a non-finite operand is invalid.
Modulo and percentile dice
The % after d specifies percentile dice, while % between expressions is the modulo operator.
For example, d% rolls a percentile die, whereas d20%3 calculates the result of a twenty-sided die modulo 3.
Order of operations โ
Expressions follow the usual mathematical order: powers are evaluated before multiplication, division and modulo, which are evaluated before addition and subtraction.
Multiplication, division and modulo have equal precedence and are evaluated from left to right. The same applies to addition and subtraction.
Use parentheses to control which parts of an expression are calculated first:
d6+2*3 // Multiply 2 by 3, then add the die result
(d6+2)*3 // Add 2 to the die result, then multiply by 3If the die rolls a 4, the first expression gives 10, while the second gives 18.
Powers are evaluated from right to left: 2^3^2 means 2^(3^2), giving 512.
Arithmetic operators are evaluated before expression comparisons.
Calculating quantities and sides โ
Use a parenthesised expression to determine how many dice to roll or how many sides they have. It can contain arithmetic, functions, dice, roll groups and nested inline rolls:
(4-2)d10 // Roll two ten-sided dice
3d(2*6) // Roll three twelve-sided diceExpressions can also include dice rolls and mathematical functions. New in v6
(d4+1)d6 // Roll a d4, add 1, then roll that many six-sided dice
2d(d6+4) // Roll a d6, add 4, then use that number of sides for two dice
(max(d4,2))d6 // Roll a d4 and use its result as the quantity, with a minimum of two
((d4)d6)d10 // Roll a d4 and use its result as the quantity of d6s, then use their total as the quantity of d10sYou can use roll groups too, including groups with modifiers:
({d4+1, d6}kh1)d8 // Keep the higher sub-roll total and roll that many eight-sided dice
2d({d6, d8}kh1+2) // Keep the higher sub-roll total, add 2, and use that number of sides for two diceFor example, in ({d4+1, d6}kh1)d8, if the d4 rolls 2 and the d6 rolls 5, the sub-roll totals are 3 and 5. The group keeps 5, so you then roll 5d8.
Each quantity and sides expression is evaluated once, before the outer pool is rolled. In 2d({d6, d8}kh1+2), if the group keeps 6, you roll 2d8; both dice use the same calculated number of sides. The dice used to calculate a parameter are not added again to the outer pool's total.
The calculated quantity must be a safe integer from 0 up to the configured maxQty limit, which defaults to 999 per set. A calculated quantity of 0 produces an empty pool with a total of 0, as in (1-1)d10. The literal notation 0d10 is still invalid. If an expression can produce a fraction, use a rounding function, for example (floor(d6/2)+1)d8.
The calculated number of sides must be a positive whole number; zero or negative sides are invalid.
The minimum and maximum roll totals evaluate the notation in minimum or maximum mode; they do not enumerate every possible parameter combination and may fail if a parameter is invalid in that mode. Where possible, averageTotal uses the resolved quantity and die from the stored roll; otherwise it falls back to the midpoint of the mode-based minimum and maximum. With random parameters or non-linear maths, it is not necessarily a statistical mean across all possible rolls.
Functions โ
Functions perform calculations on numbers, dice totals or other expressions. Use the function name followed by its arguments in parentheses.
Rounding โ
| Function | Behaviour |
|---|---|
round(x) | Round to the nearest integer; halfway values round towards positive infinity. |
floor(x) | Round down to the nearest integer. |
ceil(x) | Round up to the nearest integer. |
trunc(x) | Remove the fractional part, rounding towards zero. |
For example:
round(3.7) // 4
floor(3.7) // 3
ceil(3.7) // 4
trunc(3.7) // 3
round(3.5) // 4
floor(3.5) // 3
ceil(3.5) // 4
trunc(3.5) // 3
round(-3.7) // -4
floor(-3.7) // -4
ceil(-3.7) // -3
trunc(-3.7) // -3
round(-3.5) // -3
floor(-3.5) // -4
ceil(-3.5) // -3
trunc(-3.5) // -3Use these functions around a dice expression when you need a whole-number result:
floor(4d6/3) // Divide the dice total by 3, then round down
ceil(d20/2) // Divide the die result by 2, then round up
trunc(d6/2) // Divide the die result by 2, then discard the fractional partFloating-point rounding
Rounding functions follow JavaScript's native behaviour, without snapping values to nearby integers. For example, floor(2.9999999999999996) returns 2, and ceil((0.1+0.2)*10) returns 4 because of floating-point precision.
Other functions โ
| Function | Behaviour |
|---|---|
abs(x) | Return the absolute value. |
sign(x) | Return -1, 0 or 1 for a negative, zero or positive value. |
min(x, y) | Return the smaller of two values. |
max(x, y) | Return the larger of two values. |
pow(x, y) | Raise x to the power of y, equivalent to x^y. |
sqrt(x) | Return the square root. |
exp(x) | Return e raised to the power of x. |
log(x) | Return the natural logarithm. |
sin(x) | Return the sine of an angle in radians. |
cos(x) | Return the cosine of an angle in radians. |
tan(x) | Return the tangent of an angle in radians. |
For example:
abs(4d10-25) // Return the absolute difference between the dice total and 25
min(4d6, 20) // Use the dice total or 20, whichever is smaller
max(2d6, 5) // Use the dice total or 5, whichever is larger
pow(d6, 2) // Square the die result
sqrt(4d6) // Calculate the square root of the dice totalMathematical functions use native JavaScript numbers. Operations with no real numeric result, such as sqrt(-4), pow(-8, 1/3) or 0/0, throw an evaluation error. Positive and negative infinity are valid results, for example 1/0.
Each dice expression is rolled separately. For example, min(4d6, 2d10) rolls both sets of dice and returns the smaller total.
You can also nest functions:
floor(sqrt(4d6)) // Calculate the square root of the dice total, then round downMissing a function?
If there's a mathematical function you'd like to use that isn't supported, let us know.