Rolling dice โ
RPG Dice Roller turns a dice notation string into a completed Roll object, containing the rolled results and their total.
Make a roll โ
Create a Roll with the notation you want to roll:
import { Roll } from '@dice-roller/rpg-dice-roller';
const result = new Roll('4d6kh3');
console.log(result.total); // e.g. 14
console.log(result.present()); // e.g. 4d6kh3: [3d, 5, 6, 3] = 14new Roll() parses the notation, rolls the dice and applies modifiers immediately. result.total is the final numeric total, while result.present() formats the result for display without rolling again.
The examples assume you've installed and loaded the library. In a browser, the UMD example assumes the bundle has already been loaded; browser projects using ES modules can use the ESM form.
Using dice notation โ
Pass different notation strings for different kinds of rolls:
const attack = new Roll('d20+5');
const damage = new Roll('2d6+3');
const ability = new Roll('4d6kh3');Each constructor call creates an independent roll with new results, even if the notation is the same.
Notation supports dice types, modifiers, roll groups, maths, variables and more. See the notation guide to learn what you can roll.
Use the dice playground to try different notation strings and inspect their roll results without changing your application code.
The roll() shorthand โ
Use the exported roll() function if you prefer a function call:
import { roll } from '@dice-roller/rpg-dice-roller';
const attack = roll('d20+5');
console.log(attack.total);With the default random-number generator, roll() returns a completed Roll just like new Roll(). The difference matters when using an asynchronous external dice source.
Re-rolling โ
Call reroll() on a Roll to roll its original notation again:
const first = new Roll('2d6+3');
const second = first.reroll();
console.log(first.total);
console.log(second.total);reroll() returns a new Roll; it doesn't replace or change the original. The dice are rolled again, although they may coincidentally produce the same values.
The new roll reuses the original roll's settings and variable data by default. You can override the random engine or variable data for that re-roll. With an asynchronous dice source, reroll() returns a promise instead.
Keeping a roll history โ
Use a Roller when you want to collect rolls and reuse configuration between them:
import { Roller } from '@dice-roller/rpg-dice-roller';
const roller = new Roller();
const attack = roller.roll('d20+5');
const damage = roller.roll('2d6+3');
console.log(attack.total);
console.log(damage.total);
console.log(roller.history.length); // 2roller.roll() returns a Roll and adds it to the roller's history when it succeeds. A standalone Roll or roll() call doesn't keep a history.
See Roll history for reading, iterating, adding and clearing previous rolls. Calling reroll() on a roll from a history doesn't automatically add the new roll to that history.
Synchronous and asynchronous rolls โ
Ordinary rolls are synchronous, so there's no need to use await:
const result = new Roll('d6');
console.log(result.total);You can also supply finished die values from an external dice source. If that source is asynchronous, use Roll.create(), roll() or roller.roll() instead of new Roll():
import { Roll } from '@dice-roller/rpg-dice-roller';
const result = await Roll.create('d6', {
drawDie: {
drawMode: 'async',
callback: async ({ max }) => max,
},
});
console.log(result.total); // 6The callback above simply returns the die's highest face to demonstrate the API; a real callback might wait for an external service or a 3D dice roller.
Roll.create(), roll() and roller.roll() return a Roll for synchronous sources and a Promise<Roll> for asynchronous ones. Even an immediately resolved asynchronous callback still produces a promise. new Roll() only supports synchronous sources and throws a ValidationError if configured with an asynchronous one.
For callback arguments, validation and overrides, see External dice sources.
Working with the result โ
Every completed Roll provides the original notation, the numeric total, and an evaluated result tree containing the underlying dice results:
const result = new Roll('2d6+3');
console.log(result.notation); // '2d6+3'
console.log(result.total); // e.g. 11
console.log(result.present()); // e.g. 2d6+3: [3, 5]+3 = 11See Working with results to inspect the evaluated expression and its dice, or Presenters to control the displayed format.
Next steps โ
- Parsing and building expressions โ parse notation without rolling or construct expressions programmatically.
- Working with results โ inspect results and display rolls.
- Roll history โ keep and manage previous rolls.
- Configuring rolls โ choose engines, custom dice, modifiers, limits and variable data.
- Handling errors โ catch invalid notation and evaluation failures.
- Importing and exporting โ save rolls and restore them later.