Lcg64

public struct Lcg64 { /* private fields */ }

A 64-bit linear congruential generator. Cheap, allocation-free, and adequate for shuffling, fuzz seeds, and simulation noise — not for cryptographic use, key generation, or anything an adversary observes.

Constants come from Numerical Recipes and give a full period of 2^64:

  • multiplier a = 6364136223846793005
  • increment c = 1442695040888963407

The state update is state = state * a + c, returning the new state.

Examples

var rng = Lcg64(seed: 12345); let v1 = rng.nextUInt64(); let v2 = rng.nextUInt64(); // distinct from v1

Representation

One UInt64 field — the mutable generator state.

Initializers

public init()

Creates a generator with a hard-coded default seed (88172645463325252). Always produces the same stream — provide an explicit seed via init(seed:) when you need variation between runs.

public init(seed: UInt64)

Creates a generator initialised with seed. Different seeds produce independent streams; the same seed always reproduces the same stream (useful for deterministic tests).

Examples

var rng = Lcg64(seed: 42);

ImplementsRandomNumberGenerator

Methods

public mutating func nextInt(below: Int64) -> Int64

Returns a uniformly distributed integer in [0, upperBound). Returns 0 when upperBound <= 0 rather than panicking.

Uses naive modulo for simplicity — for upperBound close to UInt64.maxValue the result has slight bias toward smaller values. If you need exact uniformity, sample nextUInt64() and reject.

Examples

var rng = Lcg64(seed: 42); let roll = rng.nextInt(below: 6); // 0..5
public mutating func nextUInt64() -> UInt64

Advances the state once and returns the new value. O(1) and allocation-free.

ImplementsDefaultable

Initializers

init()

Builds the default-valued instance.

Defined in lang/std/numeric/random.ks