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]
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
impl DiceBuilder
Sourcepub fn from_string(input: &str) -> Result<Self, DiceBuildingError>
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)");Sourcepub fn build_from_string(input: &str) -> Result<Dice, DiceBuildingError>
pub fn build_from_string(input: &str) -> Result<Dice, DiceBuildingError>
shortcut for DiceBuilder::from_string(input).build()
Sourcepub fn reconstruct_string(&self) -> String
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.
Sourcepub fn distribution_iter(&self) -> Box<dyn Iterator<Item = (i64, BigFraction)>>
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.