Skip to content
Standard libraryZig 0.17.1Official documentation
Sourcestd/Random/lcg.zig

Source line 1

Linear congruential generator

X(n+1) = (a * Xn + c) mod m

PRNG

std.Random.lcg.std

alias

Source line 7

Alias of std

std.Random.lcg.Wrapping

type-function

Source line 11

Linear congruent generator where the modulo is std.math.maxInt(T), wrapping over the integer.

pub fn Wrapping(comptime T: type) type

Parameters

T: type

Fields

xi: T
a: T
c: T

std.Random.lcg.Wrapping.init

function

Source line 17

pub fn init(xi: T, a: T, c: T) LcgSelf

Parameters

xi: T
a: T
c: T

std.Random.lcg.Wrapping.LcgSelf

alias

Source line 26

Alias of std.Random.lcg.Wrapping

std.Random.lcg.Wrapping.next

function

Source line 21

pub fn next(lcg: *LcgSelf) T

Parameters

lcg: *LcgSelf
Source code

std/Random/lcg.zig

//! 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();
    };
}