| author | |
| committer | |
| log | d0c8fd2ce90805a6097e7b9d7a563b27e23e824c |
| tree | cde9abb9c26c25856314c968b774c1ad0d7da810 |
| parent | ad3828db45b7830d5953e7c728c227e78f574cf9 |
| signature |
2 files changed, 29 insertions(+), 0 deletions(-)
lib/std/Random.zig+1| ... | ... | @@ -26,6 +26,7 @@ pub const Sfc64 = @import("Random/Sfc64.zig"); |
| 26 | 26 | pub const RomuTrio = @import("Random/RomuTrio.zig"); |
| 27 | 27 | pub const SplitMix64 = @import("Random/SplitMix64.zig"); |
| 28 | 28 | pub const ziggurat = @import("Random/ziggurat.zig"); |
| 29 | pub const lcg = @import("Random/lcg.zig"); | |
| 29 | 30 | |
| 30 | 31 | /// Any comparison of this field may result in illegal behavior, since it may be set to |
| 31 | 32 | /// `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 | ||
| 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 | } |