pub struct Dice {
pub builder_string: String,
pub min: i64,
pub max: i64,
pub median: i64,
pub mode: Vec<i64>,
pub mean: BigFraction,
pub variance: BigFraction,
pub distribution: Vec<(i64, BigFraction)>,
pub cumulative_distribution: Vec<(i64, BigFraction)>,
pub build_time: u64,
}Expand description
A Dice represents a discrete probability distribution, providing paramters like mean, standard deviation and the roll() method to randomly sample from this distribution
A Dice is always created using a DiceBuilder. The simplest way is to use:
use dices::Dice;
let dice = Dice::build_from_string("2w6+3").unwrap();which is equivalent to
use dices::DiceBuilder;
let dice_builder = DiceBuilder::from_string("2w6+3").unwrap();
let dice = dice_builder.build();Values of the distribution are of type i64
The probabilities are of type BigFraction from the fraction crate.
This allows for precise probabilites with infinite precision, at the cost of some slower operations compared to floats, but avoids pitfalls like floating point precision errors.
Fields§
§builder_string: Stringa string that can be used to recreate the DiceBuilder that the Dice was created from.
min: i64mininum value of the probability distribution
max: i64maximum value of the probability distribution
median: i64median of the probability distribution
mode: Vec<i64>mode or modes of the probability distribution
mean: BigFractionmean of the probability distribution
variance: BigFractionvariance of the probability distribution
distribution: Vec<(i64, BigFraction)>the probability mass function (pmf) of the dice
tuples of each value and its probability in ascending order (regarding value)
cumulative_distribution: Vec<(i64, BigFraction)>the cumulative distribution function (cdf) of the dice
tuples of each value and its cumulative probability in ascending order (regarding value)
build_time: u64time it took to build the dice in microseconds
Implementations§
Source§impl Dice
impl Dice
Sourcepub fn build_from_string(input: &str) -> Result<Dice, DiceBuildingError>
pub fn build_from_string(input: &str) -> Result<Dice, DiceBuildingError>
uses the input to create a DiceBuilder and calls build() on it
Sourcepub fn builder(input: &str) -> Result<DiceBuilder, DiceBuildingError>
pub fn builder(input: &str) -> Result<DiceBuilder, DiceBuildingError>
uses the input to create a DiceBuilder. Same as [DiceBuilder::from_string(input)]
Sourcepub fn from_builder(dice_builder: DiceBuilder) -> Dice
pub fn from_builder(dice_builder: DiceBuilder) -> Dice
builds a Dice from a given DiceBuilder
this method calculates the distribution and all distribution paramters on the fly, to create the Dice.
Depending on the complexity of the dice_builder heavy lifting like convoluting probability distributions may take place here.
Sourcepub fn roll(&self) -> i64
pub fn roll(&self) -> i64
Rolls a random number for this Dice.
For this a random float is uniformly sampled over the interval [0,1) and checked against the accumulated discrete porbability distribution of this Dice.
§Examples
rolling 2 standard playing dice:
use dices::Dice;
let d : Dice = Dice::build_from_string("2d6").unwrap();
println!("rolled: {}", d.roll());
//prints something like: "rolled: 9"Sourcepub fn roll_many(&self, n: usize) -> Vec<i64>
pub fn roll_many(&self, n: usize) -> Vec<i64>
rolls the Dice n times and returns the results as a vector
Sourcepub fn prob(&self, value: i64) -> BigFraction
pub fn prob(&self, value: i64) -> BigFraction
probability that a number sampled from self is value
Sourcepub fn prob_lte(&self, value: i64) -> BigFraction
pub fn prob_lte(&self, value: i64) -> BigFraction
probability that a number sampled from self is less than or equal to value
Sourcepub fn prob_lt(&self, value: i64) -> BigFraction
pub fn prob_lt(&self, value: i64) -> BigFraction
probability that a number sampled from self is less than value
Sourcepub fn prob_gte(&self, value: i64) -> BigFraction
pub fn prob_gte(&self, value: i64) -> BigFraction
probability that a number sampled from self is greater than or equal to value
Sourcepub fn prob_gt(&self, value: i64) -> BigFraction
pub fn prob_gt(&self, value: i64) -> BigFraction
probability that a number sampled from self is greater than value