DoxigAlpha

uintLessThan

Returns an evenly distributed random unsigned integer 0 <= i < less_than. This function assumes that the underlying fillFn produces evenly distributed values. Within this assumption, the runtime of this function is exponentially distributed. If fillFn were backed by a true random generator, the runtime of this function would technically be unbounded. However, if fillFn is backed by any evenly distributed pseudo random number generator, this function is guaranteed to return. If you need deterministic runtime bounds, use uintLessThanBiased.

Function parameters

Parameters

#
T:type
less_than:T

Fast unbiased random numbers.

Types

#
DefaultPrng
Fast unbiased random numbers.
DefaultCsprng
Cryptographically secure random numbers.

Functions in this namespace

Functions

#
bytes
Read random bytes into the specified buffer until full.
enumValue
Returns a random value from an enum, evenly distributed.
enumValueWithIndex
Returns a random value from an enum, evenly distributed.
int
Returns a random int `i` such that `minInt(T) <= i <= maxInt(T)`.
uintLessThanBiased
Constant-time implementation off `uintLessThan`.
uintLessThan
Returns an evenly distributed random unsigned integer `0 <= i < less_than`.
uintAtMostBiased
Constant-time implementation off `uintAtMost`.
uintAtMost
Returns an evenly distributed random unsigned integer `0 <= i <= at_most`.
intRangeLessThanBiased
Constant-time implementation off `intRangeLessThan`.
intRangeLessThan
Returns an evenly distributed random integer `at_least <= i < less_than`.
intRangeAtMostBiased
Constant-time implementation off `intRangeAtMostBiased`.
intRangeAtMost
Returns an evenly distributed random integer `at_least <= i <= at_most`.
float
Return a floating point value evenly distributed in the range [0, 1).
floatNorm
Return a floating point value normally distributed with mean = 0, stddev = 1.
floatExp
Return an exponentially distributed float with a rate parameter of 1.
shuffle
Shuffle a slice into a random order.
shuffleWithIndex
Shuffle a slice into a random order, using an index of a
weightedIndex
Randomly selects an index into `proportions`, where the likelihood of each
limitRangeBiased
Convert a random integer 0 <= random_int <= maxValue(T),

Source

Implementation

#
pub fn uintLessThan(r: Random, comptime T: type, less_than: T) T {
    comptime assert(@typeInfo(T).int.signedness == .unsigned);
    const bits = @typeInfo(T).int.bits;
    assert(0 < less_than);

    // adapted from:
    //   http://www.pcg-random.org/posts/bounded-rands.html
    //   "Lemire's (with an extra tweak from me)"
    var x = r.int(T);
    var m = math.mulWide(T, x, less_than);
    var l: T = @truncate(m);
    if (l < less_than) {
        var t = -%less_than;

        if (t >= less_than) {
            t -= less_than;
            if (t >= less_than) {
                t %= less_than;
            }
        }
        while (l < t) {
            x = r.int(T);
            m = math.mulWide(T, x, less_than);
            l = @truncate(m);
        }
    }
    return @intCast(m >> bits);
}