random number generation
linear congruential generator
LCG
choosing A C M
algorithm parameters

Picking A, C and M for Linear congruential generator

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Linear Congruential Generators (LCGs) are a popular method for generating pseudo-random numbers. They owe their simplicity to a straightforward formula:

X_n+1=(AX_n+C)modMX\_{n+1} = (A \cdot X\_n + C) \mod M

where: • Xn+1X_{n+1} is the next random number. • XnX_n is the current random number. • AA, CC, and MM are non-negative integer constants. • MM is the modulus. • 0Xn<M0 \leq X_n < M.

To ensure that an LCG behaves as desired, particularly that it produces numbers with a long period and good statistical properties, choosing appropriate values for AA, CC, and MM is crucial.

Selecting the Modulus (M)

  1. Bit Length: Choose MM as a power of 2 or a large prime number. A common choice is $M = 2^\&#123;31\&#125;$ or $M = 2^\&#123;32\&#125;$ due to hardware efficiency.
  2. Periodicity: The maximum period of an LCG is MM when AA, CC, and MM are selected properly.

Selecting the Multiplier (A)

  1. Full Period Criterion: For full period LCGs, a common criterion is that: • A1(modp)A \equiv 1 \pmod{p} for every prime pp dividing MM. • $A \equiv 1 \pmod\&#123;4\&#125;$ if $M$ is a multiple of 4.
  2. Simple Choices: Use an increment based on the formula A=4n+1A = 4n + 1 when MM is a power of 2. However, test empirically for randomness.

Example

If M=16M = 16, a suitable multiplier can be A=5A = 5 which satisfies A1(mod4)A \equiv 1 \pmod{4}.

Selecting the Increment (C)

  1. Co-prime Criteria: CC should be co-prime to MM. This ensures the maximum period when the other conditions are met.
  2. Avoid Zero: When A1(modM)A \equiv 1 \pmod{M}, choose non-zero CC to avoid sequential repetition.

Example

For M=16M = 16 and A=5A = 5, a possible increment is C=3C = 3. Since 3 is co-prime to 16 (common factors are only 1), it is valid.

Table of Key Points

ComponentSelection CriteriaExamples
MMPrefer powers of 2 or large primes.2312^{31}, 2322^{32}, 982451653982451653
AAEnsure $A \equiv 1 \pmod\&#123;p\&#125;$ for each prime dividing M.$A=5$ for M=16M = 16
CCCo-prime with MM.C=3C=3 for M=16M=16

Further Considerations

Seed Selection (X0X_0): The seed must be chosen carefully to prevent predictable sequences. It is not bound by the co-prime rule. • Empirical Testing: Once parameters are chosen, empirical testing is essential to evaluate randomness quality using tests like the chi-square or Kolmogorov-Smirnov tests. • Modern Practices: Despite their historical popularity, LCGs are typically replaced in modern applications by more complex, cryptographically secure generators for applications requiring high-quality randomness.

In summary, the correct choice of AA, CC, and MM is crucial for the LCG to function effectively. While LCGs have limitations, when configured correctly, they serve well for applications requiring consistent and reproducible pseudo-random numbers.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms