DiceBuilder

Enum DiceBuilder 

Source
pub enum DiceBuilder {
    Constant(i64),
    FairDie {
        min: i64,
        max: i64,
    },
    SumCompound(Vec<DiceBuilder>),
    ProductCompound(Vec<DiceBuilder>),
    DivisionCompound(Vec<DiceBuilder>),
    MaxCompound(Vec<DiceBuilder>),
    MinCompound(Vec<DiceBuilder>),
    SampleSumCompound(Vec<DiceBuilder>),
}
Expand description

A DiceBuilder tree-like data structure representing the components of a dice formula like max(2d6+4,d20)

The tree can be used to calculate a discrete probability distribution. This happens when the build() method is called and creates a Dice.

§Examples

use dices::DiceBuilder;
use fraction::ToPrimitive;
let dice_builder = DiceBuilder::from_string("2d6+4").unwrap();
let dice = dice_builder.build();
let mean = dice.mean.to_f64().unwrap();
assert_eq!(mean, 11.0);

Variants§

§

Constant(i64)

A constant value (i64) that does not

§

FairDie

A discrete uniform distribution over the integer interval [min, max]

Fields

§min: i64

minimum value of the die, inclusive

§max: i64

maximum value of the die, inclusive

§

SumCompound(Vec<DiceBuilder>)

the sum of multiple DiceBuilder instances, like: d6 + 3 + d20

§

ProductCompound(Vec<DiceBuilder>)

the product of multiple DiceBuilder instances, like: d6 * 3 * d20

§

DivisionCompound(Vec<DiceBuilder>)

the division of multiple DiceBuilder instances, left-associative, rounded up to integers like: d6 / 2 = d3

§

MaxCompound(Vec<DiceBuilder>)

the maximum of multiple DiceBuilder instances, like: max(d6,3,d20)

§

MinCompound(Vec<DiceBuilder>)

the minimum of multiple DiceBuilder instances, like: min(d6,3,d20)

§

SampleSumCompound(Vec<DiceBuilder>)

SampleSumCompound(vec![a,b]) can be interpreted as follows: A DiceBuilder b is sampled a times independently of each other. It is represented by an x in input strings, e.g. “a x b” The operator is left-associative, so a x b x c is (a x b) x c.

§Examples

throwing 5 six-sided dice:

use dices::DiceBuilder::*;
let five_six_sided_dice = SampleSumCompound(
    vec![Constant(5),FairDie{min: 1, max: 6}]
);

throwing 1, 2 or 3 (randomly determined) six-sided and summing them up:

use dices::DiceBuilder::*;
let dice_1_2_or_3 = SampleSumCompound(
    vec![FairDie{min: 1, max: 3},FairDie{min: 1, max: 6}]
);

for two constants, it is the same as multiplication:

use dices::DiceBuilder::*;
let b1 = SampleSumCompound(vec![Constant(2),Constant(3)]);
let b2 = ProductCompound(vec![Constant(2),Constant(3)]);
assert_eq!(b1.build().distribution, b2.build().distribution);

Implementations§

Source§

impl DiceBuilder

Source

pub fn from_string(input: &str) -> Result<Self, DiceBuildingError>

parses the string into a tree-like structure to create a DiceBuilder

§Syntax Examples:

|—–| | | 4 six-sided dice: “4d6”

§Examples:

throwing 3 six-sided dice:

use dices::DiceBuilder;
let builder = DiceBuilder::from_string("3d6");
let builder_2 = DiceBuilder::from_string("3 d6  ");
let builder_3 = DiceBuilder::from_string("3xd6"); // explicitly using sample sum
assert_eq!(builder, builder_2);
assert_eq!(builder_2, builder_3);

the minimum and maximum of multiple dice:

use dices::DiceBuilder;
let min_builder = DiceBuilder::from_string("min(d6,d6)");
let max_builder = DiceBuilder::from_string("max(d6,d6,d20)");
Source

pub fn build(self) -> Dice

builds a Dice from [self]

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 build_from_string(input: &str) -> Result<Dice, DiceBuildingError>

shortcut for DiceBuilder::from_string(input).build()

Source

pub fn reconstruct_string(&self) -> String

constructs a string from the DiceBuilder that can be used to reconstruct an equivalent DiceBuilder from it.

currently fails to construct a correct string in case dices with a non-1 minimum are present. This is because there is no string notation for dices with a non-1 minimum yet.

Source

pub fn distribution_iter(&self) -> Box<dyn Iterator<Item = (i64, BigFraction)>>

iterator for the probability mass function (pmf) of the DiceBuilder, with tuples for each value with its probability in ascending order (regarding value)

Calculates the distribution and all distribution paramters. Depending on the complexity of [self] heavy lifting like convoluting probability distributions may take place here.

Trait Implementations§

Source§

impl Add for Box<DiceBuilder>

Source§

type Output = Box<DiceBuilder>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Debug for DiceBuilder

Source§

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

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

impl Display for DiceBuilder

Source§

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

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

impl Mul for Box<DiceBuilder>

Source§

type Output = Box<DiceBuilder>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl PartialEq for DiceBuilder

Source§

fn eq(&self, other: &DiceBuilder) -> 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 DiceBuilder

Source§

impl StructuralPartialEq for DiceBuilder

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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