Search code examples
c++randomtheory

What is the theory behind this PRNG?


__forceinline static int Random()
{
    int x = 214013, y = 2531011;
    seed = (x * seed + y);
    return ((seed >> 16) & 0x7FFF) - 0x3FFF; 
}

The code above returns PRNG with decent uniform distribution.

Now change x to x + 1 - resulting sequence couldn't be called PRNG anymore.

So what is the theory behind (this) PRNG? 'x and y are carefully chosen' but how does they were chosen?


Solution

  • This looks like a Linear congruential generator. A LCG is better when the multiplier x is divisible by all prime factors of the modulus minus one (which is 0x3FFFFFFFF here, it's a bit hidden due to the math in the return statement).