162
votes

I suspect the answer is 'Because of Math', but I was hoping someone could give a little more insight at a basic level...

I was poking around in the BCL source code today, having a look at how some of the classes I've used before were actually implemented. I'd never thought about how to generate (pseudo) random numbers before, so I decided to see how it was done.

Full source here: http://referencesource.microsoft.com/#mscorlib/system/random.cs#29

private const int MSEED = 161803398; 

This MSEED value is used every time a Random() class is seeded.

Anyway, I saw this 'magic number' - 161803398 - and I don't have the foggiest idea of why that number was selected. It's not a prime number or a power of 2. It's not 'half way' to a number that seemed more significant. I looked at it in binary and hex and well, it just looked like a number to me.

I tried searching for the number in Google, but I found nothing.

2
@48klocs: Its says so in the docs: The current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981. - Jesse Good
@48klocs Yes, page 283 here: apps.nrbook.com/c/index.html His reason seems to be "because math". - eshs
@eshs: Interesting fact: Page 283 of your link shows inextp = 31;, but the source code of Random class has it as inextp = 21; because someone mistyped it causing this bug. - Jesse Good
@Izkata We need to educate users on correct behaviour (of not voting to close erroneously) for the long-term goal of site quality, not just aim for the short-term goal (of not having a specific question closed). And if I didn't point out the above comments, it might've gotten closed as a duplicate because people do that sometimes. - Bernhard Barker

2 Answers

141
votes

No, but it's based on Phi (the "golden ratio").

161803398 = 1.61803398 * 10^8 ≈ φ * 10^8

More about the golden ratio here.

And a really good read for the casual mathematician here.

And I found a research paper on random number generators that agrees with this assertion. (See page 53.)

62
votes

This number is taken from golden ratio 1.61803398 * 10^8. Matt gave a nice answer what is this number, therefore I will just explain a little bit about an algorithm.

This is not a special number for this algorithm. The algorithm is Knuth's subtractive random number generator algorithm and the main points of it are:

  • store a circular list of 56 random numbers
  • initialization is process of filling the list, then randomize those values with a specific deterministic algorithm
  • two indices are kept which are 31 apart
  • new random number is the difference of the two values at the two indices
  • store new random number in the list

The generator is based on the following recursion: Xn = (Xn-55 - Xn-24) mod m, where n &geq; 0. This is a partial case of lagged Fibonacci generator: Xn = (Xn-j @ Xn-k) mod m, where 0 < k < j and @ is any binary operation (subtraction, addition, xor).

There are several implementations of this generator. Knuth offers an implementation in FORTRAN in his book. I found the following code, with the following comment:

PARAMETER (MBIG=1000000000,MSEED=161803398,MZ=0,FAC=1.E-9)

According to Knuth, any large MBIG, and any smaller (but still large) MSEED can be substituted for the above values.

A little bit more can be found here Note, that this is not actually a research paper (as stated by Math), this is just a master degree thesis.

People in cryptography like to use irrational number (pi, e, sqrt(5)) because there is a conjecture that digits of such numbers appears with equal frequency and thus have high entropy. You can find this related question on security stackexchange to learn more about such numbers. Here is a quote:

"If the constants are chosen at random, then with high probability, no attacker will be able to break it." But cryptographers, being a paranoid lot, are skeptical when someone says, "Let's use this set of constants. I picked them at random, I swear." So as a compromise, they'll use constants like, say, the binary expansion of π. While we no longer have the mathematical benefit of having chosen them at random from some large pool of numbers, we can at least be more confident there was no sabotage.