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.

lcg(seed, n, m = 65537L, a = 75L, c = 74L)

Arguments

seed

initial starting value

n

the number of random numbers you want to create.

m

modulus argument. By default, \(m = 2^{16} + 1\) (follows ZX81 configuration)

a

multiplier argument. By default, \(a = 75\) (follows ZX81 configuration)

c

increment argument By default, \(c = 74\) (follows ZX81 configuration)

Details

The generator is defined by the recurrence relation: $$X_{n+1} = (aX + c) \text{ mod } m$$ Where \(X\) is the sequence of pseudo-random values and

- \(m, 0 < m\) - the "modulus"

- \(a, 0 < a < m\) - the "multiplier"

- \(c, 0 \le c < m\) - the "increment"

- \(X_0, 0 \le X_0 < m\) - the "seed" or "start value"

For more information see the Wikipedia page.

Examples

random_numbers <- lcg(1234, 1000)
# Plot numbers to see that they are random
plot(random_numbers)