rand-select

Crates.iorand-select
lib.rsrand-select
version0.2.0
created_at2025-04-15 16:37:39.656445+00
updated_at2025-07-25 06:24:45.190806+00
descriptiona tiny choice selector
homepage
repositoryhttps://github.com/Canop/rand-select
max_upload_size
id1634768
size10,864
Denys Séguret (Canop)

documentation

README

MIT Latest Version docs Chat on Miaou

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.

Commit count: 2

cargo fmt