tabatkins / roll

🎲 An exact-results dice-rolling library, for answering dice-related stat questions.
MIT License
14 stars 0 forks source link

Roll.js

🎲 An exact-results dice-rolling library, for answering dice-related stat questions.

I'm an avid D&D player, as is my brother, and we both like working on homebrew design as well. Part of this requires understanding dice results very well, so you can tell if something is likely to be balanced against something else.

Previously, I'd often answer dice questions by quickly writing up some simulation code-- run 10k trials and take the average, and your result will be good enough. But it bugged me having to rewrite that same code over and over, handling multiple dice was trickier than I thought it could be, and ultimately it just kinda bugged me that I was using approximations when I knew the exact result wasn't tricky to calculate, it just required a bit of work.

So, I wrote this library for myself, which offers a fairly convenient JS API for calculating dice results precisely, even for complicated operations involving multiple dice, rerolls, and complex reactions to dice results.

See the dingus for a script playground, along with several pieces of example code showing off the functionality.

Roll class

The Roll class represents the result of a die roll (or several). It maintains a list of possible results (usually, die faces) paired with the chance of each occurring.

For example, Roll.d6 has a list of six such pairs, each containing a value from 1-6 and a chance of 1/6 (approximately .16666).

Constructing Rolls

There are several ways to construct rolls:

Manipulating Rolls

Once a Roll has been constructed, there are several methods for altering the result.

If the convenience methods above aren't enough, you can get low-level and manipulate Rolls more directly:

Displaying Rolls

Once you've gotten a Roll, you probably want to know its results.