| Crates.io | rand-select |
| lib.rs | rand-select |
| version | 0.2.0 |
| created_at | 2025-04-15 16:37:39.656445+00 |
| updated_at | 2025-07-25 06:24:45.190806+00 |
| description | a tiny choice selector |
| homepage | |
| repository | https://github.com/Canop/rand-select |
| max_upload_size | |
| id | 1634768 |
| size | 10,864 |
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.