2 unstable releases
Uses new Rust 2024
| 0.2.0 | Jul 25, 2025 |
|---|---|
| 0.1.0 | Apr 15, 2025 |
#928 in Data structures
90 downloads per month
6KB
54 lines
The RandomSelector selects among weighted choices, without bias.
use rand_select::RandomSelector;
let selector = RandomSelector::default()
.with(1.0, 'A')
.with(1.5, 'B')
.with_none(3.0);
let l = selector.select();
// l has half a chance to be None, and is 50% more likely to be 'B' than 'A'
If you set a value and call neither with_none nor with_none_up_to, the selector will always return a value.
If you have already normalized weight, with_none_up_to is a convenient way to set the total weight of the selector:
use rand_select::RandomSelector;
let selector = RandomSelector::default()
.with(0.1, 'A')
.with(0.2, 'B')
.with_none_up_to(1.0);
The RandomSelector is designed for reuse, and can use the RNG of your choice.
Dependencies
~395KB