Uniform ranges v0.1.0
UniformBelow<G>
pub func UniformBelow<G: RandomGenerator>(
generator: &var G,
bound: uint64
) -> uint64
A uniform value in [0, bound).
A bound of zero has no values to choose from and gives zero, which is the only answer that is not a lie.
Runs in constant expected time; the loop below runs more than once with probability under bound / 2^64.
Type parameters
| Name | Description |
|---|---|
G: RandomGenerator | the generator's type |
Parameters
| Name | Type | Description |
|---|---|---|
generator | &var G | where the bits come from |
bound | uint64 | the exclusive upper bound |
Returns: a value in [0, bound), each equally likely and with no modulo bias
UniformInRange<G>
pub func UniformInRange<G: RandomGenerator>(
generator: &var G,
low: uint64,
high: uint64
) -> uint64
A uniform value in [low, high], both ends included.
Inclusive because that is what a caller asking for a die roll or a random index into a known last element
means. low above high is an empty range and gives low. The full range of the type is representable:
a span of every value is a bound of 2^64, which does not fit in a uint64, and is handled as its own case
rather than by an arithmetic accident.
Type parameters
| Name | Description |
|---|---|
G: RandomGenerator | the generator's type |
Parameters
| Name | Type | Description |
|---|---|---|
generator | &var G | where the bits come from |
low | uint64 | the inclusive lower bound |
high | uint64 | the exclusive upper bound |
Returns: a value in [low, high), each equally likely
UniformInSignedRange<G>
pub func UniformInSignedRange<G: RandomGenerator>(
generator: &var G,
low: int64,
high: int64
) -> int64
A uniform signed value in [low, high], both ends included.
The span is computed in unsigned arithmetic, so a range crossing zero — or spanning the whole width, where
the difference of the ends overflows a signed type — is ordinary rather than a special case.
Type parameters
| Name | Description |
|---|---|
G: RandomGenerator | the generator's type |
Parameters
| Name | Type | Description |
|---|---|---|
generator | &var G | where the bits come from |
low | int64 | the inclusive lower bound |
high | int64 | the exclusive upper bound |
Returns: a value in [low, high), each equally likely
UniformFloatInRange<G>
pub func UniformFloatInRange<G: RandomGenerator>(
generator: &var G,
low: float64,
high: float64
) -> float64
A uniform float64 in [low, high).
The interval is half-open at the top, matching NextFloat64. A reversed pair gives low, and an interval so
wide that its span overflows to an infinity gives values across the whole of it rather than a NaN.
Type parameters
| Name | Description |
|---|---|
G: RandomGenerator | the generator's type |
Parameters
| Name | Type | Description |
|---|---|---|
generator | &var G | where the bits come from |
low | float64 | the inclusive lower bound |
high | float64 | the exclusive upper bound |
Returns: a value in [low, high)