Linear Congruential Generator

LCG is a basic known-seed Pseudo-Random Number Generator which follows this mathematical formula:

Xn+1​=(aXn​+c)modmXn+1​=(aXn​+c)modm

where:

  • X is the sequence of pseudo-random values,

  • mm is the modulus,

  • aa is the multiplier,

  • cc is the increment,

  • X0X0 is the seed or start value.

  • nn represents the step or position in the sequence of pseudorandom numbers being generated.

  • XnXn is the current pseudorandom number at step n.

  • Xn+1Xn+1 is the next pseudorandom number in the sequence, generated from Xn.

For example, a=5, c=2, m=123, X0=73

The numbers generated by the formula: will be in the range from 0 to m-1.

The seed value initializes the PRNG's internal state. For an LCG, this is the initial value X0 from which the sequence of pseudorandom numbers begins.

Drawback: The PRNG is deterministic, meaning the sequence it generates is entirely determined by the seed. Even though the numbers appear random, if you know the seed and the algorithm's parameters, you can predict the entire sequence of numbers that will be generated. So, it is not very secure.

Here is a code and description above

Last updated

Was this helpful?