# How can I determine the period of my pseudo-random number generator?

Suppose I'm using a linear congruential pseudo-random number generator (PRNG). Given a seed $x_0$, the multiplying factor (a), the shift factor (c) and the modulus factor (m), how can I determine the period of my PRNG? Do I determine it by experimentation / pattern detection algorithms, or is there a direct formula for calculating its period?

Although my question is specifically about the linear congruential method, I am open to knowing more about how periods are calculated in practice for other PRNG's as well.

• Relevant: en.wikipedia.org/wiki/… – user182 Mar 4 '12 at 17:46
• BTW, if you're using LFSR then the period is maximal iff the feedback polynomial is primitive. In such case, the period AFAIK (don't quote me; too lazy to dig up my course notes) is $q^n$ where the feedback polynomial $p(x) \in \mathbb{F}_{q}[x]$ of degree $n$, and $q$ is the size of the field of coefficients. – user182 Mar 6 '12 at 17:31
• Floyd's cycle detecting algorithms as well as Brent's cycle detection algorithms are both efficient ways to detect cycles. Both will return some multiple L of the period, and once you have that, you can factorize L and see which is the smallest factor that is a period. – xdavidliu Jul 23 '18 at 17:51

## 1 Answer

If you constrain yourself to full cycle LCG PRNGs then the answer is easy, by definition it's simply $$m$$.

To find the period of a non full cycle LCG PRNG for a given seed you just need to count the number of iterations of the PRNG until it generates the seed value once more.

From the referenced wikipedia page:

### Period length

The period of a general LCG is at most $$m$$, and for some choices of a much less than that. Provided that $$c$$ is nonzero, the LCG will have a full period for all seed values if and only if:

• $$c$$ and $$m$$ are relatively prime,
• $$a - 1$$ is divisible by all prime factors of $$m$$
• $$a - 1$$ is a multiple of 4 if $$m$$ is a multiple of 4.

While LCGs are capable of producing decent pseudorandom numbers, this is extremely sensitive to the choice of the parameters $$c$$, $$m$$, and $$a$$.

Historically, poor choices had led to ineffective implementations of LCGs. A particularly illustrative example of this is RANDU which was widely used in the early 1970s and lead to many results which are currently being questioned because of the use of this poor LCG.

# Why you want to use a full cycle generator

If you don't constrain yourself to full cycle LCG PRNGs then you are taking a huge risk.

If you don't know that a given LCG is full cycle then you could end up with a generator with an arbitrary number of mutually distinct sequences, some of which could be embarrassingly small and have appalling randomness, possibly even worse than the infamous RANDU generator.

You really don't want to have to check every possible seed value to make sure that it generates a sequence which is long enough for your application.

# Further reading

For an excellent primer on pseudo random number generators, I would strongly recommend that you read the Numerical Recipes chapter on Random Numbers.

• That is true, but I'm not constraining myself to full period LCG PRNG's... I'm curious about the poor choices of a,c, and m, such that the random streams that do not achieve full period. I'd like to be able to know ahead of time, given some a,c, and m, what will the period inevitably be. I know that it is upper bounded by m, but I was wondering if we can do better than that and obtain the exact period. – Paul Mar 5 '12 at 14:34
• I don't think this is splitting hairs on a technicality at all: the question was "how to determine the period of an LCG with arbitrary parameters", while this answer says "don't use arbitrary LCG's, always use full period LCGs, and assuming you do, the answer to your question would be the maximum possible period, by definition". The argument for using full-period LCFs laid out in this answer is perfectly convincing, but the issue is that's not at all what the question asked. – xdavidliu Jul 23 '18 at 15:28
• Sorry @xdavidliu but I don't see how your new comment helps me improve my answer. You brought to my attention that I didn't actually answer the question, I edited my answer to fix that, and then let you know in a way I thought might make you smile (if you're a Futurama fan). I don't see that anything more needs to be said. – Mark Booth Jul 23 '18 at 16:09
• Note that on stack exchange, comments are not intended for extended discussions, for that use Computational Science Chat. Comments are for helping to improve questions and answers, and are distracting, so we try to keep them to a minimum. Comments should be considered ephemeral, any comment which no longer actively helps to improve a question or answer may be deleted at any time to tidy up a post. – Mark Booth Jul 23 '18 at 16:09