6

I have a pseudo-random number generator, that I am trying to understand. I have no relevant knowledge about math. I'm hoping that you can identify if this is a well-known algorithm and what its name is, so I can read a little about it.

This is the algorithm:

int random_seed = 1;

void random() { random_seed = 16807 * random_seed + (random_seed / 127773) * -2147483647;

if (random_seed == 0) {
    random_seed = std::numeric_limits<int>::max();
}

}

int main() { for (int x = 0; x < 10; x++) { random(); std::cout << random_seed << std::endl; // This prints the results on the screen } }

Which prints the following pseudo-random generated numbers on the screen:

16807
282475249
1622650073
984943658
1144108930
470211272
101027544
1457850878
1458777923
2007237709

I found out that 16807 is related to the Lehmer random number generator, which is defined as:

${\displaystyle X_{k+1}=a\cdot X_{k}{\bmod {m}},}$

where $a$ is the multiplier 16807
and $X_{k}$ would be the random_seed.

$m$ is the modulus which in Lehmer's is $2^{31}-1$ and would be $2{,}147{,}483{,}647$. However, in the code example it is $-2{,}147{,}483{,}647$.

However, $m$ is not used as a modulus but as a factor. And it is not $2{,}147{,}483{,}647$ but $-2{,}147{,}483{,}647$. Then there's the question about the addition of the quotient of $X_{k} / 127773$. I haven't really found something helpful to me when searching for number $127773$.

Do you recognize any well-known pseudo-random number generator in the provided code? What could it be?

Daniel
  • 163
  • 127773 is the result of the integer division 2147483647 / 16807, as I found here. – Stefan Dec 17 '23 at 13:45
  • Linear Congruential Generators (LCG) are fast, but they aren't great. However, there are fast ways to improve them. See https://www.pcg-random.org/ – PM 2Ring Dec 18 '23 at 05:07

1 Answers1

17

Yes, this is an implementation of the 1969 Lewis-Goodman-Miller linear congrential generator $$X_{n+1}=7^5 X_n\bmod 2^{31}{-}1$$ described in a paper by Park and Miller. The funny computer code with the obscure constants is discussed in the paper. It's intended to work on computers with 32 bit integers, for which naive implementation will cause overflow.

kimchi lover
  • 24,277