How to use the lcg random sequence calculator
- Enter the modulus m, the multiplier a, the increment c and the seed.
- Enter how many terms to list.
- Read the period, whether the generator reaches every value, and the first terms.
- Try the presets to compare a full period setting with a poor one.
Formula
Next value = (a x current value + c) mod m
Full period needs: c and m share no factor other than 1
a - 1 is divisible by every prime factor of m
a - 1 is divisible by 4 when m is divisible by 4
These three conditions are the Hull and Dobell theorem. When they hold, the sequence visits every value from 0 to m - 1 before repeating.
m = 16, a = 5, c = 3, seed 7
The terms are 7, then (5 x 7 + 3) mod 16 = 6, then 1, then 8, and so on. The period is 16, which is the full range, so all of the numbers from 0 to 15 appear once before the cycle repeats. The tool confirms that the Hull and Dobell test passes.
Changing to a = 4 and c = 2 with seed 1 gives 1, 6, 10, 10, 10 and so on. The sequence gets stuck on 10 after a few steps, so the period is 1, and the Hull and Dobell test fails.
How a linear congruential generator works
An LCG makes each number from the one before it with a multiplication, an addition and a remainder. It is fast and needs very little memory, which made it popular in early software. The quality depends on the constants. A bad choice can have a short cycle or visible patterns, as the second example shows.
The period is the number of steps before the sequence starts to repeat. The tool finds it by a cycle detection method that works for large moduli without storing every value, and it also reports how many steps come before the cycle begins.
Do not use it for security
Because the next value is a simple function of the last, anyone who sees a few outputs can predict all the rest. LCGs are fine for simulations, games and teaching, and unsuitable for passwords, keys or anything an attacker might guess. The lowest bits of a power of two modulus are especially regular. For everyday random numbers see the Random Number Generator, and to test whether a sequence looks random try the Runs Test for Randomness Calculator. Other sequences are in the Collatz Sequence Calculator and the Arithmetic and Geometric Sequence Calculator.
Assumptions and limits
- All values are whole numbers, and the modulus is at least 2.
- Numbers up to 9,007,199,254,740,991 are handled exactly.
- The constants shown are examples. Use the ones you want to study.
- Up to 200 terms are listed, though the period is found for any size.
Frequently asked questions
What does full period mean?
The sequence visits each of the m possible values exactly once per cycle, so no value is skipped or repeated early.
Can the increment be zero?
Yes. That form is called multiplicative. It cannot reach a full period of m, because the value 0 would stay at 0.
Does the seed change the period?
With a full period setting no. With other settings the seed can lead to different cycles of different lengths.
Are the classic 32 bit constants good?
They reach the full period of 4,294,967,296 and are fine for simple uses, though far from the best modern generators.