wicker

Crates.iowicker
lib.rswicker
version0.2.0
sourcesrc
created_at2022-05-03 16:23:28.253191
updated_at2022-10-11 23:14:49.013445
descriptionWeighted probability picker for Rust
homepage
repository
max_upload_size
id579912
size12,763
petrak@ (gamma-delta)

documentation

README

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
Commit count: 0

cargo fmt