authorgravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2026-01-13 22:16:51+01:00
committergravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2026-01-14 12:48:09+01:00
logd0c8fd2ce90805a6097e7b9d7a563b27e23e824c
treecde9abb9c26c25856314c968b774c1ad0d7da810
parentad3828db45b7830d5953e7c728c227e78f574cf9
signaturebadge-check Signed by SSH key SHA256:p3IHbr0lyK2ekfDC1Zi7dOEV/9T6lGghNawhl5sBnM4

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


2 files changed, 29 insertions(+), 0 deletions(-)

lib/std/Random.zig+1
......@@ -26,6 +26,7 @@ pub const Sfc64 = @import("Random/Sfc64.zig");
2626pub const RomuTrio = @import("Random/RomuTrio.zig");
2727pub const SplitMix64 = @import("Random/SplitMix64.zig");
2828pub const ziggurat = @import("Random/ziggurat.zig");
29pub const lcg = @import("Random/lcg.zig");
2930
3031/// Any comparison of this field may result in illegal behavior, since it may be set to
3132/// `undefined` in cases where the random implementation does not have any associated
lib/std/Random/lcg.zig created+28
......@@ -0,0 +1,28 @@
1//! Linear congruential generator
2//!
3//! X(n+1) = (a * Xn + c) mod m
4//!
5//! PRNG
6
7const std = @import("std");
8
9/// Linear congruent generator where the modulo is `std.math.maxInt(T)`,
10/// wrapping over the integer.
11pub fn Wrapping(comptime T: type) type {
12 return struct {
13 xi: T,
14 a: T,
15 c: T,
16
17 pub fn init(xi: T, a: T, c: T) LcgSelf {
18 return .{ .xi = xi, .a = a, .c = c };
19 }
20
21 pub fn next(lcg: *LcgSelf) T {
22 lcg.xi = (lcg.a *% lcg.xi) +% lcg.c;
23 return lcg.xi;
24 }
25
26 const LcgSelf = @This();
27 };
28}