weighted_list/lib.rs
1//! A list implementation for weighted randomisation.
2//!
3//! This crate provides the [`WeightedList<V,W>`](WeightedList) struct, which stores values each with assigned weights. When randomly selecting items, those with a higher weight are more likely to be chosen.
4//!
5//! ## Example
6//!
7//! ```rust
8//! use weighted_list::*;
9//!
10//! let wl = WeightedList::<String, u8>::from([
11//! (2, "sup".to_string()),
12//! (3, "nova".to_string()),
13//! (5, "shard".to_string()),
14//! ]);
15//!
16//! for item in &wl {
17//! println!("{} has weight {}", item.value, item.weight);
18//! }
19//!
20//! if let Some(result) = wl.select_random_value(&mut rand::rng()) {
21//! println!("{result}");
22//! }
23//! ```
24//!
25//! ## Why might you need this?
26//!
27//! - Weighted randomisation for a reward system
28//! - Item stacking for an inventory system
29//! - Statistical sampling
30//!
31//! For more detailed guidance on how to use the struct, see [`WeightedList`].
32
33mod root;
34pub use root::{
35 Weight,
36};
37
38mod weighted_item;
39pub use weighted_item::{
40 WeightedItem,
41 WItem
42};
43
44mod weighted_list;
45pub use weighted_list::{
46 WeightedList,
47 WList
48};