Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
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 standard library. Public Function

limitRangeBiased

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

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

Params

  • T: type
  • random_int: T
  • less_than: T

File

Random.zig:441
441
442
443
444
445
446
447
448
449
450
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);
}