Skip to content

@randsum/roller

Roll dice with a single function call — numbers, notation strings, or options objects. @randsum/roller is the core engine that powers the entire RANDSUM ecosystem: full TypeScript types, strict input validation, and a single dependency (@randsum/notation).

  • Three input modes — pass a number, a notation string, or a typed options object
  • Advanced notation — drop/keep, reroll, exploding, compounding, penetrating, cap, replace, unique, count successes
  • Modifier pipeline — modifiers execute in a defined priority order, composable and predictable
  • Multiple arguments — combine rolls into a single total: roll('1d20+5', '2d6')
  • Strict validation — throws on invalid input; validate user strings with isDiceNotation() or validateNotation()
  • Custom RNG — pass a randomFn for seeded, deterministic, or cryptographic randomness
  • Universal runtime — works in Node.js, Bun, Deno, browsers, and edge functions

Pass a number to roll one die with that many sides.

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

Pass a Randsum Dice Notation string for complex rolls.

import { roll } from '@randsum/roller'
// 4d6, drop the lowest
const abilityScore = roll('4d6L')
// 2d20, keep highest (advantage), add 5
const attack = roll('2d20K+5')
// Exploding d6s with rerolls below 3
const wild = roll('4d6!R{<3}')

Pass a typed object for full control over the roll without notation parsing.

import { roll } from '@randsum/roller'
const result = roll({
sides: 6,
quantity: 4,
modifiers: {
drop: { lowest: 1 },
plus: 2
}
})
console.log(result.total) // Sum of best 3d6 + 2

All three input types produce the same RollerRollResult — you can mix them in a single call:

import { roll } from '@randsum/roller'
// Combine multiple arguments into one total
const result = roll('1d20+5', '2d6')
console.log(result.total) // d20 + 5 + 2d6
PatternDescriptionExample
NdSRoll N dice with S sides4d6
+X / -XAdd/subtract from total1d20+5
L / HDrop lowest/highest4d6L
K / klKeep highest/lowest4d6K3
!Exploding dice3d6!
!!Compounding exploding3d6!!
!pPenetrating exploding3d6!p
UUnique results4d20U

See the full Randsum Dice Notation Spec for reroll conditions, caps, replacements, success counting, and multipliers.