weighted_list/lib.rs
1//! A list implementation for weighted randomisation.
2//!
3//! This crate provides the `WeightedList<V,W>` struct, which can assign weights to values. When selecting items randomly, items 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!("{item}");
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;
34
35mod weighted_item;
36pub use weighted_item::{
37 WeightedItem,
38 WItem
39};
40
41mod weighted_list;
42pub use weighted_list::{
43 WeightedList,
44 WList
45};