Is 161803398 A 'Special' Number? Inside of Math.Random()

C#.NetAlgorithmRandom

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRob P.View Question on Stackoverflow
Solution 1 - C#Matt Johnson-PintView Answer on Stackoverflow
Solution 2 - C#Salvador DaliView Answer on Stackoverflow