Skip to main content

Crate dicebag

Crate dicebag 

Source
Expand description

Dice rolling!

In most cases, anything from i8/u8 up to i128/u128 and usize is supported (alongside f32/f64 for a few functions).

§The Extensions

§DiceExt

Covers the core intent dice rollings, e.g. 3.d6(), 2.d10().

use dicebag::DiceExt;
let a = 3.d6();
let b = 2_u8.d4();
let c = 5.d(a-3); // FYI: zero as dice size results in 0, no matter the number of dice...

§HiLo

Coin flipping, using whatever datatype you implement it for… Comes with two convenience macros that dynamically infer their return type based on your assignment context:

use dicebag::lo;
 
if lo!() { /* do something if result was "low" */ }

§InclusiveRandomRange

To get a random value within given range.

use dicebag::InclusiveRandomRange;
let range = 6..=12;
let v = range.random_of();

§RandomOf<T>

A trait to get some random entry from e.g. Vec or HashSet (or some other sequential whatsoever, if you decide to explicit impl the RandomOf<T> for it).

Just make sure your container has at least one entry in it as otherwise things will catch fire (panic). .random_of() really can’t choose a random element out of nothing given…

use dicebag::RandomOf;
let v = vec![2,4,6,8,10];
let x: i32 = v.random_of();
 
#[derive(Clone)]
struct Abc { tag: String };
let abc = vec![Abc{tag:"a".into()}, Abc{tag:"b".into()}, Abc{tag:"c".into()}];
let x = abc.random_of();
assert!(x.tag == "a" || x.tag == "b" || x.tag == "c");

§KeyedRandomOf<K,T>

As per RandomOf<T>, but for e.g. HashMaps.

Just make sure your container has at least one entry in it as otherwise things will catch fire (panic). .random_of() really can’t choose a random element out of nothing given…

Macros§

hi
Roll some arbitrary dice and see if their result is “high”.
lo
Roll some arbitrary dice and see if their result is “low”.
percentage_chance_of
$chance% of $v, otherwise 0.

Enums§

DiceRollMatrix
Dice roll matrix for e.g. serde parsing, etc.
DiceRollMatrixMod
Dice roll matrix mod for e.g. serde parsing, etc.

Traits§

DiceExt
Dice extensions.
FixedNumberVariance
Fixed value value variator(s).
HiLo
Couple “coin-flip” extensions…
InclusiveRandomRange
IsOne
“It’s just one, isn’t it?”…
KeyedAllocatorRandomOf
KeyedRandomOf
PercentageVariance
Percentage amount value variator(s).
PlainRandomOf
RandomOf

Type Aliases§

DiceT