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