Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

limitRangeBiased

Convert a random integer 0 <= random_int <= maxValue(T), into an integer 0 <= result < less_than. This function introduces a minor bias.

Random.limitRangeBiased
pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T

File

lib/std/Random.zig:441

Code

pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T {
    comptime assert(@typeInfo(T).int.signedness == .unsigned);
    const bits = @typeInfo(T).int.bits;

    // adapted from:
    //   http://www.pcg-random.org/posts/bounded-rands.html
    //   "Integer Multiplication (Biased)"
    const m = math.mulWide(T, random_int, less_than);
    return @intCast(m >> bits);
}