Crate rand_select

Source
Expand description

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.

Structsยง

RandomSelector
A selector allowing to randomly select a value from a set of choices, each with an associated weight.