4 releases (2 breaking)
|0.8.2||Feb 10, 2021|
|0.8.0||Apr 8, 2020|
|0.7.0||Feb 22, 2020|
|0.1.0||Feb 21, 2020|
#89 in Game dev
988 downloads per month
Used in 8 crates (4 directly)
Part of the
bracket-random is focused on providing dice-oriented random numbers. It also (optionally) includes parsing of RPG-style dice strings (e.g.
3d6+12). It is targeted at games, particularly RPGs. It uses the high-performance
XorShift algorithm for random number generation.
bracket-random, include the following in your
[dependencies] bracket-random = "0.8.2"
It will be available as a crate very soon.
You can use
bracket-random by including the
prelude and instantiating
use bracket_random::prelude::*; let mut rng = RandomNumberGenerator::new();
You can also seed the RNG with
There are a number of random options available:
rng.roll_dice(1, 6)rolls a six-sided die once.
rng.roll_dice(3, 6)rolls three six-sided die, and adds them up.
rng.roll_str("3d6+12")rolls three six-sided die, adds them up, and adds twelve to the result.
rng.next_u64provides a random
u64, anywhere within the
rng.rand::<TYPE>()tries to provide a random number of type
rng.range(min, max)tries to provide a random number within the specified range.
Noneif there are no options, or the index of a randomly selected slice entry. This is handy for treasure tables.
Nonefor empty slices, or the contents of a randomly selected slice entry.
bracket-random library includes a dice string parser. You can try to parse a string as follows:
use bracket_random::prelude::*; let dice_type = parse_dice_string("3d6-4");
This returns a
Result, which will either be
Ok or a parsing error. If unwrapped, it provides a
DiceType structure, breaking out the details of the requested die roll.
5d6-1 formats. If you turn off the
parsing feature flag, this feature is excluded - but your project won't be bloated by regular expression libraries and
parsingenables parsing of dice types as strings.
- If you are compiling for
wasm32-unknown-unknown, it automatically includes
Execute examples with
cargo run --example <name>.
dicerollrolls 3d6 (specified as
roll_dice(1,6)) 10 times and prints the results.
dicestringrolls 3d6 (specified as
roll_str("3d6")) 10 times and prints the results.
distributionrolls 3d6, 200,000 times and plots the distribution of each cumulative result.
nextobtains the next 10
u64random numbers and prints them.
randobtains the next 10
f64random numbers and prints them. This demonstrates how the
randfunction can take any type that the underlying
randomlibrary considers sufficiently numeric.
rangeobtains the next 10 random numbers in the range
100.200and prints them.
slice_indexrandomly picks a slice index, prints it - and the contents of the array that was sliced.
slicerandomly picks a slice entry and prints it.
die_iteratoruses the (new and in need of work)
DiceIteratorfunction to roll 10d6 in a compact manner.