mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 01:04:43 +01:00
feat(std.Random): add a linear congruent generator
This commit is contained in:
parent
ad3828db45
commit
d0c8fd2ce9
2 changed files with 29 additions and 0 deletions
|
|
@ -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
28
lib/std/Random/lcg.zig
Normal 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();
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue