Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the theory behind this PRNG?

Tags:

c++

random

theory

__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?

like image 210
Vadim Avatar asked Dec 21 '22 07:12

Vadim


1 Answers

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

like image 72
MSalters Avatar answered Dec 24 '22 02:12

MSalters