Uniform ranges v0.1.0

Source
Uniform integers and floats in a bounded range.

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.

Source

Type parameters

NameDescription
G: RandomGeneratorthe generator's type

Parameters

NameTypeDescription
generator&var Gwhere the bits come from
bounduint64the 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.

Source

Type parameters

NameDescription
G: RandomGeneratorthe generator's type

Parameters

NameTypeDescription
generator&var Gwhere the bits come from
lowuint64the inclusive lower bound
highuint64the 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.

Source

Type parameters

NameDescription
G: RandomGeneratorthe generator's type

Parameters

NameTypeDescription
generator&var Gwhere the bits come from
lowint64the inclusive lower bound
highint64the 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.

Source

Type parameters

NameDescription
G: RandomGeneratorthe generator's type

Parameters

NameTypeDescription
generator&var Gwhere the bits come from
lowfloat64the inclusive lower bound
highfloat64the exclusive upper bound

Returns: a value in [low, high)