Dice

Struct Dice 

Source
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: String

a string that can be used to recreate the DiceBuilder that the Dice was created from.

§min: i64

mininum value of the probability distribution

§max: i64

maximum value of the probability distribution

§median: i64

median of the probability distribution

§mode: Vec<i64>

mode or modes of the probability distribution

§mean: BigFraction

mean of the probability distribution

§variance: BigFraction

variance 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: u64

time it took to build the dice in microseconds

Implementations§

Source§

impl Dice

Source

pub fn build_from_string(input: &str) -> Result<Dice, DiceBuildingError>

uses the input to create a DiceBuilder and calls build() on it

Source

pub fn builder(input: &str) -> Result<DiceBuilder, DiceBuildingError>

uses the input to create a DiceBuilder. Same as [DiceBuilder::from_string(input)]

Source

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.

Source

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"
Source

pub fn roll_many(&self, n: usize) -> Vec<i64>

rolls the Dice n times and returns the results as a vector

Source

pub fn prob(&self, value: i64) -> BigFraction

probability that a number sampled from self is value

Source

pub fn prob_lte(&self, value: i64) -> BigFraction

probability that a number sampled from self is less than or equal to value

Source

pub fn prob_lt(&self, value: i64) -> BigFraction

probability that a number sampled from self is less than value

Source

pub fn prob_gte(&self, value: i64) -> BigFraction

probability that a number sampled from self is greater than or equal to value

Source

pub fn prob_gt(&self, value: i64) -> BigFraction

probability that a number sampled from self is greater than value

Source

pub fn prob_all(&self, value: i64) -> ProbAll

returns prob_lt, prob_lte, prob, prob_gte, prob_gt in the [ProbAll] struct. Computes them more efficiently than if we use all the functions individually.

Source

pub fn quantile<T: ToFloat>(&self, p: T) -> i64

returns the smallest p-quantile of the distribution. The smallest p-quantile q is the smallest value in the distribution for which it holds, that P(x ≤ q) ≥ p currently the trait [ToFloat] is implementen for [BigFraction] and f64

Trait Implementations§

Source§

impl Debug for Dice

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Dice

Source§

fn eq(&self, other: &Dice) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Dice

Source§

impl StructuralPartialEq for Dice

Auto Trait Implementations§

§

impl Freeze for Dice

§

impl RefUnwindSafe for Dice

§

impl Send for Dice

§

impl Sync for Dice

§

impl Unpin for Dice

§

impl UnwindSafe for Dice

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V