d20tek-dicenotation 5.2.1

dotnet add package d20tek-dicenotation --version 5.2.1
                    
NuGet\Install-Package d20tek-dicenotation -Version 5.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="d20tek-dicenotation" Version="5.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="d20tek-dicenotation" Version="5.2.1" />
                    
Directory.Packages.props
<PackageReference Include="d20tek-dicenotation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add d20tek-dicenotation --version 5.2.1
                    
#r "nuget: d20tek-dicenotation, 5.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package d20tek-dicenotation@5.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=d20tek-dicenotation&version=5.2.1
                    
Install as a Cake Addin
#tool nuget:?package=d20tek-dicenotation&version=5.2.1
                    
Install as a Cake Tool

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:

  • RandomDieRoller uses System.Random. It accepts a seed for reproducible rolls.
  • CryptoDieRoller uses the cryptographic random number generator.
  • MathNetDieRoller uses MathNet.Numerics random sources. It also accepts a seed. It ships in the separate d20tek-dicenotation-mathnet package, so apps that don't use it don't take on the MathNet.Numerics dependency.
  • ConstantDieRoller always 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 Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on d20tek-dicenotation:

Package Downloads
d20tek-dicenotation-blazor

Blazor component that animates d20Tek.DiceNotation roll results as physics-driven dice on an HTML canvas. The rolled values are determined by the dice library and the animation simply presents them, so the visuals never change an outcome.

d20tek-dicenotation-mathnet

Adds a die roller for d20Tek.DiceNotation that uses Math.NET Numerics random sources, including a seeded, replayable Mersenne Twister roller.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.2.1 119 11/22/2026
5.1.9 150 10/28/2026
5.1.8 115 10/12/2026
5.1.7 154 9/20/2026
5.1.6 138 9/15/2026
5.1.5 201 6/11/2026
5.1.4 314 4/28/2026
5.1.3 180 3/27/2026
5.1.2 190 2/13/2026
5.1.1 190 1/14/2026
5.0.2 396 11/13/2025
4.0.7 419 9/18/2025
4.0.6 364 8/4/2025
4.0.5 294 7/4/2025
4.0.1 1,446 2/7/2024
4.0.0 321 1/25/2024
3.8.2 374 11/16/2023
3.4.2 445 4/7/2023
3.3.2 671 12/13/2021
3.2.3 748 11/24/2020
Loading failed

Current release:
Upgrade library packages to .NET 10.

Release 1.0.5:
* Add dice notation for keep lowest by using L token in the notation. Changed drop lowest notation to use P token.

Release 1.0.4:
* Added tracking service to keep track of die rolls to provide statistical data.
* Added abstract RandomDieRollerBase class for shared code between all random rollers, so those classes can just implement minimally the number generation.

Release 1.0.3:
* Added value converters for DiceResult and TermResultList to help with default display of these classes as text.
* Two bug fixes:
- Added IDice.Clear method to allow dice to be reused with new expressions (without needing to recreating instance).
- Dice.Parse now throws an exception if null or empty string is used.

Release 1.0.2:
* Added support percentile (d%) notation.
* Added support for dropping lowest N dice (similar to keeping highest N dice).
* Added support for Fudge/FATE dice notation.
* Added support for exploding or pentrating dice rolls.
* Added support for default dice sides (3d =>3d6), and ability to set default number of sides as dice config.

Release 1.0.1:
* Added general purpose parser to handle more math and dice expressions.
* Added support for grouping using ( ). Now you can parse dice expressions like - (2+1)d4 - (4-2).
* Test validation for new expressions and grouping.