feat(std.Random): add a linear congruent generator

This commit is contained in:
GasInfinity 2026-01-13 22:16:51 +01:00
parent ad3828db45
commit d0c8fd2ce9
No known key found for this signature in database
2 changed files with 29 additions and 0 deletions

View file

@ -26,6 +26,7 @@ pub const Sfc64 = @import("Random/Sfc64.zig");
pub const RomuTrio = @import("Random/RomuTrio.zig");
pub const SplitMix64 = @import("Random/SplitMix64.zig");
pub const ziggurat = @import("Random/ziggurat.zig");
pub const lcg = @import("Random/lcg.zig");
/// Any comparison of this field may result in illegal behavior, since it may be set to
/// `undefined` in cases where the random implementation does not have any associated

28
lib/std/Random/lcg.zig Normal file
View file

@ -0,0 +1,28 @@
//! Linear congruential generator
//!
//! X(n+1) = (a * Xn + c) mod m
//!
//! PRNG
const std = @import("std");
/// Linear congruent generator where the modulo is `std.math.maxInt(T)`,
/// wrapping over the integer.
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();
};
}