Skip to content

Quick Start

This guide walks you through the basics of using @randsum/roller to roll dice in your application.

import { roll } from '@randsum/roller'
// Roll a single d20
const result = roll(20)
console.log(result.total) // 1-20

The roll() function accepts several argument types. A plain number means “roll one die with that many sides.”

RANDSUM supports standard dice notation. The format is NdS where N is the number of dice and S is the number of sides.

import { roll } from '@randsum/roller'
// Roll 2d6
roll('2d6')
// Roll 4d8
roll('4d8')
// Roll 1d20
roll('1d20')

Drop the lowest or highest dice from a roll:

// D&D ability score: 4d6, drop lowest
roll('4d6L')
// Advantage: 2d20, keep highest (drop lowest)
roll('2d20L')
// Disadvantage: 2d20, keep lowest (drop highest)
roll('2d20H')
// Drop 2 lowest
roll('4d6L2')

Add or subtract from the roll total:

// Attack roll with +5 modifier
roll('1d20+5')
// Damage with -1 penalty
roll('2d6-1')
// Reroll any 1s
roll('4d6R{1}')
// Reroll results below 3
roll('4d6R{<3}')
// Exploding dice (reroll and add on max)
roll('3d6!')

Chain multiple modifiers together:

// Reroll 1s, drop lowest, add 2
roll('4d6R{1}L+2')
// Exploding dice with drop
roll('3d6!L')

Every notation string has an equivalent options object form:

// These are equivalent:
roll('4d6L')
roll({
sides: 6,
quantity: 4,
modifiers: { drop: { lowest: 1 } }
})

The options object gives you full programmatic control when notation strings are not flexible enough.

The roll() function returns a result object with useful properties:

const result = roll('4d6L+2')
result.total // Final total after all modifiers
result.rolls // Array of individual roll records

Roll different dice types in a single expression:

// Attack + damage in one call
roll('1d20+5', '2d6+3')