[−][src]Module randomize::bounded
Module for bounded randomization.
This module has two types for ranges. They each have some quirks.
DebiasedIntMulRange
can't be made to cover the full range of the integer type it's a range for. It also takes a division to construct, so you maybe don't want to be making them "on the fly" as much since division is slow.BitmaskRange
is fairly cheap to construct and also can cover the full range of the integer type used. However, if the range "width" between low and high isn't very close to the next higher power of two (eg,1..=20
is only 19 out of 32) then there will be many rejected values.
My advice is to use DebiasedIntMulRange
as much as you can, and fall back
to BitmaskRange
only when necessary. The vital operation, convert
is
provided via a trait so that functions can be generic over the exact range
type used.
Note: The two different range types might or might not generate
different results with their convert
function using a particular size of
range. All that is assured is that a uniform input will give uniform output.
The exact translation from input to output is not the same between the two
range types.
Structs
BitmaskRange | A random range that uses bit masking and rejection to avoid bias. |
DebiasedIntMulRange | A random range that uses multiplication and rejection to avoid bias. |
Traits
RandomRange | Things that produce an unbiased value within some range. |