Working with results โ
A completed Roll contains more than a total. You can inspect the individual dice, see how modifiers affected them and access the evaluated parts of the notation.
Roll properties โ
import { Roll } from '@dice-roller/rpg-dice-roller';
const roll = new Roll('2d6+3');
console.log(roll.notation); // '2d6+3'
console.log(roll.total); // e.g. 11| Property | Description |
|---|---|
notation | The original notation, or the generated notation if rolling a programmatically built expression. |
total | The final numeric total, rounded to two decimal places. |
result | The evaluated expression tree, including individual dice results. |
expression | The abstract syntax tree (AST) describing the expression that was evaluated. |
description | The first description in the expression, if there is one. |
minTotal, maxTotal, averageTotal | Calculated totals, or null when unavailable. |
For example, you can retrieve a description without extracting it from the notation or the formatted output:
const damage = new Roll('2d6 [Fire damage]');
console.log(damage.description?.text); // 'Fire damage'Displaying results โ
Call present() to format a completed roll:
const roll = new Roll('4d6kh3');
console.log(roll.present());
// e.g. 4d6kh3: [3d, 5, 6, 3] = 14The default presenter returns a string containing the notation, rolled values, modifier symbols and total. You can also use String(roll) or roll.toString(), which always use the built-in text presenter, even if you've configured a different presenter.
Formatting a result doesn't roll the dice again. For different output formats, including graphical results, see Presenters.
If you need the numeric values or modifier information in your application, use the result tree rather than parsing the formatted string.
The result tree โ
roll.result describes how the expression was evaluated. Each expression result has a type identifying the kind of expression, a node containing its source AST node, and a numeric value.
For a simple pool such as 2d6, the top-level result is a pool. For 2d6+3, it is a binary result containing a pool on the left and a number on the right.
const roll = new Roll('2d6+3');
const result = roll.result;
console.log(result.type); // 'binary'
if (result.type === 'binary') {
console.log(result.left.type); // 'pool'
console.log(result.right.type); // 'number'
console.log(result.right.value); // 3
}For example, if the dice roll 3 and 5, the result tree has this structure (omitting the AST nodes and other properties for clarity):
binary (value: 11)
โโโ left: pool (value: 8)
โ โโโ rolls: [3, 5]
โโโ right: number (value: 3)The tree follows the structure of the expression rather than storing all dice in a single array. Check the type before accessing properties specific to a result. TypeScript also uses this check to narrow the result's type.
The top-level result.value is the evaluated numeric value before the two-decimal-place rounding applied to roll.total.
Dice pools โ
A pool result contains the evaluated quantity (qty), evaluated die arguments (dieArgs), information about the resolved die (die), and its individual dice results (rolls).
const roll = new Roll('4d6kh3');
if (roll.result.type === 'pool') {
const pool = roll.result;
console.log(pool.qty.value); // 4
console.log(pool.die.max); // 6
console.log(pool.rolls.map(die => die.value)); // e.g. [3, 5, 6, 3]
console.log(pool.value); // e.g. 14
}The die property is a snapshot of the resolved die, including its notation, minimum, maximum, average and face count. It isn't the die factory or a live die instance.
Individual dice โ
Each item in pool.rolls is a DieResult, with the following properties:
| Property | Description |
|---|---|
type | Always 'die'. |
initialValue | The original value of this result, before modifications. |
value | The value after modifiers have been applied. |
calculationValue | The numeric contribution used when calculating the pool total. |
useInTotal | Whether the contribution is included in the pool total. |
flags | Semantic markers describing modifier effects on the result. |
These values are normally the same for a die without modifiers, apart from useInTotal (which is true) and flags (which is an empty array).
Modified values โ
A modifier can change the value of a die without losing its original value. For example, with d6min3, a roll of 1 is raised to 3 by the Min modifier:
| Property | Example value |
|---|---|
initialValue | 1 |
value | 3 |
calculationValue | 3 |
useInTotal | true |
flags | ['min'] |
A normal single-die re-roll similarly preserves the original value while replacing the current value. However, initialValue isn't a complete record of every random draw. For example, branching re-rolls can replace one result with several new results, and compounded explosions combine values. The result tree contains the finished results, not a log of all draws.
Scoring and excluded dice โ
Some modifiers change the contribution to a total without changing the die's displayed value. With 3d6>=5, for example, each matching die contributes one success instead of its face value.
Suppose the dice roll 2, 5 and 6:
value | calculationValue | useInTotal | flags |
|---|---|---|---|
2 | 0 | true | [] |
5 | 1 | true | ['target-success'] |
6 | 1 | true | ['target-success'] |
The total is 2, not 13. Weighted targets and failures can also produce contributions greater than one or less than zero.
Keep and Drop exclude dice using useInTotal. Dropped dice remain in the pool's results; their calculationValue needn't be zero.
To retrieve the dice that are included in a pool total:
const roll = new Roll('4d6kh3');
if (roll.result.type === 'pool') {
const kept = roll.result.rolls.filter(die => die.useInTotal);
console.log(kept.map(die => die.value));
}The pool's value is the sum of calculationValue for the results whose useInTotal is true. Don't simply sum every die's value, or you may ignore the effects of modifiers.
Modifier flags โ
Flags are descriptive names rather than the symbols you see in the text output. For example, the 'explode' flag is displayed as ! by the default text presenter, and 'drop' as d.
const roll = new Roll('4d6kh3');
if (roll.result.type === 'pool') {
const dropped = roll.result.rolls.filter(die => die.flags.includes('drop'));
console.log(dropped.map(die => die.value));
}A result can have several flags. Flags alone don't determine its contribution: use calculationValue and useInTotal for scoring. For a list of built-in modifiers and their behaviour, see Modifiers.
Roll groups โ
A roll group contains members rather than a direct array of dice. Each member has its own evaluated expression under result, together with value, calculationValue, useInTotal and flags.
const roll = new Roll('{2d6+3, d20}kh1');
if (roll.result.type === 'group') {
for (const member of roll.result.members) {
console.log(member.result.type); // 'binary', then 'pool'
console.log(member.value);
console.log(member.useInTotal);
}
}Group modifiers may score or exclude an entire member, without changing its nested dice results. As with a pool, the group's value is the sum of the included members' calculationValue values.
Some group modifiers operate on the dice within a member instead (see Roll groups). When examining nested dice, remember that an enclosing member can be excluded even when the individual dice have useInTotal set to true.
Calculated dice quantities, sides and modifiers โ
Expressions used to calculate dice quantities or sides are themselves evaluated results.
const roll = new Roll('(d4)d6');
if (roll.result.type === 'pool') {
console.log(roll.result.qty.value); // e.g. 3
console.log(roll.result.rolls.length); // 3, if the d4 rolled 3
}The d4 result is nested inside qty; it isn't one of the d6 results in rolls. Similarly, calculated sides are found in dieArgs.
Calculated Keep and Drop counts are recorded in the optional modifierArguments array on a pool or group. Each entry contains a modifierIndex (the index in the owning AST node's modifier array) and the evaluated result.
These nested results determine how the enclosing expression is evaluated; their values aren't automatically added to its total.
Finding dice in any expression โ
For arbitrary notation, traverse the result tree according to the type of each expression. The following function collects every pool in the evaluated tree, including pools used to calculate quantities, sides and modifier arguments:
function* findPools(result) {
switch (result.type) {
case 'pool':
yield result;
yield* findPools(result.qty);
for (const arg of result.dieArgs) {
yield* findPools(arg);
}
break;
case 'group':
for (const member of result.members) {
yield* findPools(member.result);
}
break;
case 'binary':
case 'comparison':
yield* findPools(result.left);
yield* findPools(result.right);
break;
case 'unary':
yield* findPools(result.operand);
break;
case 'paren':
yield* findPools(result.inner);
break;
case 'function':
for (const arg of result.args) {
yield* findPools(arg);
}
break;
case 'conditional':
yield* findPools(result.condition);
yield* findPools(result.selectedResult);
break;
}
for (const arg of result.modifierArguments ?? []) {
yield* findPools(arg.result);
}
}
const roll = new Roll('2d6+d8+3');
for (const pool of findPools(roll.result)) {
console.log(pool.rolls.map(die => die.value));
}Number literals and variable references have no nested results. A conditional has only its evaluated condition and selected branch; the unselected branch wasn't evaluated.
This example is useful for inspecting all evaluated pools, not for reconstructing the order of random draws or calculating the final total by adding their values together. A nested pool may be used as a parameter, excluded by a group modifier or involved in a mathematical operation.
Result objects are intended to be treated as readonly data. Changing the tree directly won't recalculate the stored total. Use a new roll or re-roll to obtain fresh results.
Calculated totals โ
In addition to the actual total, a roll has three calculated statistics:
minTotal: the lowest possible total, rounded to two decimal places.maxTotal: the highest possible total, rounded to two decimal places.averageTotal: the average result across all possible rolls, accounting for their probabilities, without rounding intermediate values.
const roll = new Roll('2d6+3');
console.log(roll.total); // e.g. 11
console.log(roll.minTotal); // 5
console.log(roll.maxTotal); // 15
console.log(roll.averageTotal); // 10Each property is calculated on first access and cached, including when the value is null. Calculating statistics does not roll dice again, consume random numbers or call an external dice source.
Supported calculations โ
The library accounts for arithmetic and supported modifiers rather than simply rolling every die at its minimum or maximum face. Averages take the probabilities of the possible outcomes into account, including repeated faces in a face die.
| Notation | Minimum | Maximum | Average (approx.) |
|---|---|---|---|
10-d6 | 4 | 9 | 6.5 |
d6*d6 | 1 | 36 | 12.25 |
3d{1,1,6} | 3 | 18 | 8 |
2d20kh1 | 1 | 20 | 13.825 |
4d6dl1 | 3 | 18 | 12.2446 |
4d6=3 | 0 | 4 | 0.6667 |
Supported cases include:
- Standard, percentile, Fudge, range and face dice with fixed or deterministically calculated quantities and faces.
- Addition, subtraction, multiplication, unary negation and unmodified roll groups. Division supports bounds when the denominator's range excludes zero; its average requires a fixed, non-zero denominator.
- Dice pools with Min, Max, target success/failure (including weighted targets), critical success/failure and Sort modifiers.
- A single Keep or Drop modifier on a dice pool, including deterministically calculated counts.
Unavailable statistics โ
Each statistic has the type number | null. If a calculation is not supported, its value is null rather than an estimate or the midpoint of the minimum and maximum.
const roll = new Roll('10/d6');
console.log(roll.minTotal); // 1.67
console.log(roll.maxTotal); // 10
console.log(roll.averageTotal); // nullRepeating modifiers (such as Explode, Re-roll and Unique), modified roll groups, combinations of Keep or Drop with other modifiers, random dice parameters, comparisons, conditionals and many mathematical functions have limited or no statistical support. Calculations requiring large face distributions or expensive Keep/Drop analysis may also be unavailable.
This does not prevent the notation from being rolled normally. Check for null before using a statistic, and remember that zero is a valid value.
console.log(roll.averageTotal ?? 'Unavailable');Custom dice and modifiers โ
Statistics for built-in dice and modifiers describe their default behaviour, even if you replace their factories or handlers, change modifier execution order or supply values using an external dice source. They may therefore differ from the actual behaviour of a customised roll.
For separately registered custom dice, simple calculations can use the die's declared minimum, maximum and average. Calculations requiring its full probability distribution may be unavailable.
Full exports preserve stored statistics, including null, when imported again.
Next steps โ
- Roll history โ work with a collection of rolls and their combined total.
- Presenters โ customise how results are displayed.
- Importing and exporting โ save and restore completed results.