线性同余生成器
X(n+1) = (a * Xn + c) mod m
伪随机数生成器
std.Random.lcg.std
别名
别名目标 std
std.Random.lcg.Wrapping
类型函数
模数为 std.math.maxInt(T) 的线性同余生成器,
在整数范围上回绕。
pub fn Wrapping(comptime T: type) type
参数
T: type
字段
xi: T
a: T
c: T
std.Random.lcg.Wrapping.init
函数
pub fn init(xi: T, a: T, c: T) LcgSelf
参数
xi: T
a: T
c: T
std.Random.lcg.Wrapping.LcgSelf
别名
std.Random.lcg.Wrapping.next
函数
pub fn next(lcg: *LcgSelf) T
参数
lcg: *LcgSelf
源码
std/Random/lcg.zig
//! 线性同余生成器
//!
//! X(n+1) = (a * Xn + c) mod m
//!
//! 伪随机数生成器
const std = @import("std");
/// 模数为 `std.math.maxInt(T)` 的线性同余生成器,
/// 在整数范围上回绕。
pub fn Wrapping(comptime T: type) type {
return struct {
xi: T,
a: T,
c: T,
pub fn init(xi: T, a: T, c: T) LcgSelf {
return .{ .xi = xi, .a = a, .c = c };
}
pub fn next(lcg: *LcgSelf) T {
lcg.xi = (lcg.a *% lcg.xi) +% lcg.c;
return lcg.xi;
}
const LcgSelf = @This();
};
}