rand-select 0.1.0

a tiny choice selector
Documentation
  • Coverage
  • 71.43%
    5 out of 7 items documented3 out of 7 items with examples
  • Size
  • Source code size: 10.24 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.52 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 18s Average build duration of successful builds.
  • all releases: 18s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Canop/rand-select
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Canop

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.