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.
Linear Congruential Generators (LCGs) are a popular method for generating pseudo-random numbers. They owe their simplicity to a straightforward formula:
where: • is the next random number. • is the current random number. • , , and are non-negative integer constants. • is the modulus. • .
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 , , and is crucial.
Selecting the Modulus (M)
- Bit Length: Choose as a power of 2 or a large prime number. A common choice is
$M = 2^\{31\}$ or $M = 2^\{32\}$due to hardware efficiency. - Periodicity: The maximum period of an LCG is when , , and are selected properly.
Selecting the Multiplier (A)
- Full Period Criterion: For full period LCGs, a common criterion is that: • for every prime dividing . •
$A \equiv 1 \pmod\{4\}$ if $M$is a multiple of 4. - Simple Choices: Use an increment based on the formula when is a power of 2. However, test empirically for randomness.
Example
If , a suitable multiplier can be which satisfies .
Selecting the Increment (C)
- Co-prime Criteria: should be co-prime to . This ensures the maximum period when the other conditions are met.
- Avoid Zero: When , choose non-zero to avoid sequential repetition.
Example
For and , a possible increment is . Since 3 is co-prime to 16 (common factors are only 1), it is valid.
Table of Key Points
| Component | Selection Criteria | Examples |
| Prefer powers of 2 or large primes. | , , | |
Ensure $A \equiv 1 \pmod\{p\}$ for each prime dividing M. | $A=5$ for | |
| Co-prime with . | for |
Further Considerations
• Seed Selection (): 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 , , and 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
- Picking a random element from a set
- Picking a random element from a set
- Planar Graph Layouts
- Please explain murmur hash?
- Please explain the logic behind Kernighan's bit counting algorithm
- Please identify this algorithm probabilistic top-k elements in a data stream
- Please tell me the efficient algorithm of Range Mex Query
- Point and ellipse rotated position test algorithm

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 courseTrack 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.