DiceCli 5.2.1
dotnet tool install --global DiceCli --version 5.2.1
dotnet new tool-manifest
dotnet tool install --local DiceCli --version 5.2.1
#tool dotnet:?package=DiceCli&version=5.2.1
nuke :add-package DiceCli --version 5.2.1
d20Tek.DiceNotation
d20Tek.DiceNotation is a .NET library for parsing, evaluating, and rolling dice notation. It targets .NET 9 and .NET 10, and it is trimming- and Native AOT-compatible, so you can use it in web, desktop, console, and game clients alike.
Dice notation (also known as dice algebra, common dice notation, or RPG dice notation) is a simple,
algebra-like way to describe combinations of dice in role-playing games, such as 2d6+12 or
4d6k3. The library supports standard and percentile dice, Fudge/FATE dice, keep highest/lowest,
drop lowest, exploding and penetrating dice, grouping, and math operators. See the
dice notation specification and the
dice notation examples for the full syntax.
Installation
The library is published on NuGet as d20tek-dicenotation.
.NET CLI:
dotnet add package d20tek-dicenotation
Package Manager Console:
PM > Install-Package d20tek-dicenotation
Math.NET Die Roller
MathNetDieRoller, which uses Math.NET Numerics random sources, is in its own package so that the
core library doesn't depend on MathNet.Numerics:
dotnet add package d20tek-dicenotation-mathnet
Visual Dice Roller for Blazor
If you are building a Blazor application and want to animate your rolls, the companion package renders physics-driven dice on an HTML canvas:
dotnet add package d20tek-dicenotation-blazor
The dice library still decides every outcome and the component only presents it, so an animation never changes a result. See the Blazor introduction, Blazor getting started, and Blazor API reference for details.
Usage
Dice notation strings
Parse and roll a notation string in one call:
IDice dice = new Dice();
DiceResult result = dice.Roll("4d6k3+2");
Console.WriteLine($"{result.DiceExpression} = {result.Value} [{result.RollsDisplayText}]");
Invalid notation does not throw. Check result.HasError and result.Error instead.
Building expressions in code
You can also build an expression with a fluent API:
IDice dice = new Dice();
// equivalent of the dice notation: 4d6k3 + d8 + 5
var expression = DiceExpression.Create()
.AddDice(6, 4, choose: 3)
.AddDice(8)
.AddConstant(5);
DiceResult result = dice.Roll(expression);
Batch rolls
Roll the same notation or expression many times, for simulations and statistics:
IReadOnlyList<DiceResult> results = dice.RollMany("1d20+5", 1000);
Roll breakdowns
Each die records whether it was kept, dropped, exploded, or rerolled. Use KeptResults,
DroppedResults, ExplodedResults, and RerolledResults on DiceResult, or VerboseDisplayText
for an annotated description of the roll.
Dependency injection
Register IDice, its configuration, the die roller, and an optional roll tracker with
AddDiceNotation():
services.AddDiceNotation(options => options
.UseCryptoDieRoller()
.UseRollTracker());
See the dependency injection and factory reference for all options.
Die rollers
Rolls use RandomDieRoller by default. You can pass a different roller to any Roll call, or
configure one through dependency injection:
RandomDieRollerusesSystem.Random. It accepts a seed for reproducible rolls.CryptoDieRolleruses the cryptographic random number generator.MathNetDieRolleruses MathNet.Numerics random sources. It also accepts a seed. It ships in the separated20tek-dicenotation-mathnetpackage, so apps that don't use it don't take on the MathNet.Numerics dependency.ConstantDieRolleralways returns the same value, which is useful in unit tests.
Seeded rollers implement IReplayableDieRoller and expose a ReplayToken that
DieRollerFactory.CreateFromReplayToken uses to replay the same sequence of rolls. See the
reproducible rolling guide. You can also implement IDieRoller
to plug in your own random number generator.
Roll tracking
DieRollTracker and AggregateRollTracker record the frequency of each die value, and can save and
load that data as JSON.
Documentation
Samples
- DiceCli: a command-line dice roller.
- BlazorDice: a Blazor WebAssembly app that shows the visual dice roller.
Building and testing
You need the .NET 10 SDK. From the repository root:
dotnet build
dotnet test
The test projects use MSTest on Microsoft.Testing.Platform. To collect code coverage for a test project, run:
dotnet test --project tests/d20Tek.DiceNotation.UnitTests -- --coverage --coverage-output-format cobertura
Performance benchmarks live in benchmarks/d20Tek.DiceNotation.Benchmarks.
Feedback
If you use this library and have feedback, bugs, or suggestions, please file them in the Issues section of this repository.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
Initial version with ability to roll dice based on known dice notation.