wicker 0.2.0

Weighted probability picker for Rust
Documentation
  • Coverage
  • 84.62%
    11 out of 13 items documented2 out of 12 items with examples
  • Size
  • Source code size: 16.04 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.91 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 19s Average build duration of successful builds.
  • all releases: 19s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • gamma-delta

Wicker

It's often helpful to have weighted probabilities. This crate offers the [WeightedPicker] struct that serves as a sort of weighted bag; you can give it entries with various weights, and then randomly sample them.

This is the way Minecraft loot tables work, if this sounds familiar.

The algorithm used is Vose's Alias Method (scroll to the bottom), which to be honest I absolutely do not understand. But it has O(n) creation and O(1) selection, so sounds good to me.

A [WeightedPicker] is static; you can't edit the probabilities after you've created it due to the algorithm used. However, you can edit each associated value after creation through the [WeightedPicker::pick] method, if you wanted to do that for some reason.

Sample Usage

# use wicker::WeightedPicker;
let picker = WeightedPicker::new(vec![
    ("common", 10.0),
    ("uncommon", 5.0),
    ("rare", 2.0),
    ("legendary", 1.0),
    ("mythic", 0.1),
]);

let mut rng = rand::thread_rng();
for _ in 0..10 {
    println!("- {}", picker.get(&mut rng));
}

A sample output:

  • legendary
  • rare
  • uncommon
  • common
  • common
  • rare
  • uncommon
  • common
  • common
  • uncommon