Linear congruential generator

Two modulo-9 LCGs show how different parameters lead to different cycle lengths. Each row shows the state evolving until it repeats. The top row shows a generator with m = 9, a = 2, c = 0, and a seed of 1, which produces a cycle of length 6. The second row is the same generator with a seed of 3, which produces a cycle of length 2. Using a = 4 and c = 1 (bottom row) gives a cycle length of 9 with any seed in [0, 8].

A linear congruential generator (LCG) is an algorithm that yields a sequence of pseudo-randomized numbers calculated with a discontinuous piecewise linear equation. The method represents one of the oldest and best-known pseudorandom number generator algorithms. The theory behind them is relatively easy to understand, and they are easily implemented and fast, especially on computer hardware which can provide modular arithmetic by storage-bit truncation.

The generator is defined by the recurrence relation:

where is the sequence of pseudo-random values, and

— the "modulus"
— the "multiplier"
— the "increment"
— the "seed" or "start value"

are integer constants that specify the generator. If c = 0, the generator is often called a multiplicative congruential generator (MCG), or Lehmer RNG. If c ≠ 0, the method is called a mixed congruential generator.[1]: 4- 

When c ≠ 0, a mathematician would call the recurrence an affine transformation, not a linear one, but the misnomer is well-established in computer science.[2]: 1 

  1. ^ Cite error: The named reference KnuthV2 was invoked but never defined (see the help page).
  2. ^ Steele, Guy L. Jr.; Vigna, Sebastiano (February 2022) [15 January 2020]. "Computationally easy, spectrally good multipliers for congruential pseudorandom number generators". Software: Practice and Experience. 52 (2): 443–458. arXiv:2001.05304. doi:10.1002/spe.3030. hdl:2434/891395. these denominations, by now used for half a century, are completely wrong from a mathematical viewpoint.... At this point it is unlikely that the now-traditional names will be corrected. Associated software and data at https://github.com/vigna/CPRNG.