Skip to content

Latest commit

 

History

136 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

diceystats

This crate is used to sample from and analyze dice formulas which use dice notation, in the style of Dungeons and Dragons. For more info, read the documentation.

Example

use diceystats::{
    dices::DiceFormula,
    dist::{DenseDist, Dist},
    roll,
};

// Roll a four side die and a five sided die, sum the result.
// Then roll that number of six sided dice, and sum those rolls.
let example = "(d4 + d5)xd6";

// We can roll the dice and calculate the result
let result: isize = roll(example, &mut rand::rng()).unwrap();
assert!((2..=54).contains(&result));

// Or to do more advanced things we can parse it into a `DiceFormula`
let d4_d5_d6: DiceFormula = example.parse().unwrap();

// Then we can calculate its probability distribution
let dist: DenseDist<f64> = d4_d5_d6.dist();
assert!((dist.mean() - 19.25).abs() <= 0.01);

// If we want to be more precise we can use arbitrary precision numbers
use num::BigRational;

let dist: DenseDist<BigRational> = d4_d5_d6.dist();
assert_eq!(dist.mean().to_string(), "77/4");

About

Rust crate for analyzing distributions from dice formulas

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages