163

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.

Rob P.
  • 14,921
  • 14
  • 73
  • 109
  • possible duplicate of [Two different seeds producing the same 'random' sequence](http://stackoverflow.com/questions/13364401/two-different-seeds-producing-the-same-random-sequence) – MethodMan May 15 '14 at 20:39
  • the option was possible duplicate not specifically a duplicate – MethodMan May 15 '14 at 20:42
  • Googling for it, I find references to a PRNG from Knuth (where that constant is referred to as MSEED). – 48klocs May 15 '14 at 20:52
  • 6
    @48klocs: Its says so in the [docs](http://msdn.microsoft.com/en-US/library/system.random.aspx): `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 May 15 '14 at 20:56
  • 4
    @48klocs Yes, page 283 here: http://apps.nrbook.com/c/index.html His reason seems to be "because math". – eshs May 15 '14 at 20:56
  • 22
    @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](http://connect.microsoft.com/VisualStudio/feedback/details/634761/system-random-serious-bug). – Jesse Good May 15 '14 at 21:31
  • @Dukeling When you VTC as duplicate, the comment is automatically added as "possible duplicate of" specifically _because_ it needs more votes by other users who are in agreement. – Izkata May 16 '14 at 01:00
  • 8
    @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 May 16 '14 at 01:11

2 Answers2

142

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

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 17
    Do you know why a number based on Phi makes a good choice as a seed? Would it be possible to summarize this here? – Bernhard Barker May 15 '14 at 20:57
  • 29
    @Dukeling The constant is used exactly once, to temper the incoming seed. My very strong suspicion is that it was chosen to be a [nothing up my sleeve number](http://en.wikipedia.org/wiki/Nothing_up_my_sleeve_number) that prevents seeds with few bits set (perhaps a common choice) from screwing up the random number generator (instead of some magical property of phi). – David Eisenstat May 15 '14 at 21:09
  • 7
    To quote a quote from the said book _According to Knuth any large MBIG and any smaller (but still large) MSEED can be substituted for the above values._ So it is mathematical fun, more or less.. So the correct answer should be: _No. But it is based on Phi._ – TaW May 15 '14 at 21:19
  • I actually have no idea why it is chosen. I just recognized Phi. However, one could also argue that it only *approximates* Phi, since that's an irrational number and we only are using 8 decimals of precision here. – Matt Johnson-Pint May 15 '14 at 21:55
  • One might speculate that since Phi is found in nature, it might make a good choice of seed to approximate randomness. But I am not a good enough mathematician to offer a proof of that. – Matt Johnson-Pint May 15 '14 at 22:00
  • If the seed were perfectly random, then the choice of `MSEED` would not matter at all. – David Eisenstat May 15 '14 at 22:05
  • @PrestonGuillot Just use `int random = new Random(new Random().Next(new Random().Next(), new Random().Next)).Next(new Random().Next(), new Random().Next())`. You're guarenteed to get ___BIG___ random! – Cole Tobin May 16 '14 at 16:32
  • 14
    Just took a look through that random number paper - this line stood out a little - `"One can’t even fathom the repercussions if security flaws in the implementation (or design) of the SSL protocol are to be found."` (page 4) – jcw May 16 '14 at 19:10
  • 2
    I would think that a more relevant way to use the golden ratio would be to use (modulus/phi) rather than using a base-10 representation of the digits in code that has nothing to do with base 10. An interesting characteristic of phi which I didn't see on that page is that from what I can tell, for any integer N, the value N/phi-int(N/phi) >= 1/N/sqrt(5). That would mean that even if one generated a sequence of numbers N/phi-int(N/phi), the distance between the the closest pair will be within a factor of sqrt(5) of the largest possible uniform spacing in the interval (0..1) – supercat May 16 '14 at 23:01
62

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.

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 5
    As for the answser, it's not just because of their entropy, it's also because those numbers double as [nothing up my sleeve](http://en.wikipedia.org/wiki/Nothing_up_my_sleeve_number) numbers. – Cole Tobin May 16 '14 at 16:31