Function limitRangeBiased [src]
Convert a random integer 0 <= random_int <= maxValue(T),
into an integer 0 <= result < less_than.
This function introduces a minor bias.
Prototype
pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T
Parameters
T: type
random_int: T
less_than: T
Source
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);
}