# What is golden ratio doing in this computer code?

In this file (related to random number generation), there is following line:

  private const int MSEED = 161803398;


which resembles golden ratio.

How come golden ratio plays a role in random number generation? Can you help me understand this from mathematical point of view?

NOTE: Naturally, I predict there will be many people saying: This is a programming question. However, I am asking for answer/clarification/insight from math point of view. Hope you can make this subtle distinction.

## Update

After some research, I found that the most likely origin of the code I linked to at the top is the following comment and code: (from the book NUMERICAL RECIPIES, pg. 198):

Finally, we give you Knuth's suggestion for a portable routine, which we have translated to the present conventions as RAN3. This is not based on the linear congruential method at all, but rather on a subtractive method. One might hope that its weaknesses, if any, are therefore of a highly different character from the weaknesses, if any, of RAN1 above [not given]. If you ever suspect trouble with one routine, it is a good idea to try the other in the same application. RAN3 has one nice feature: if your machine is poor on integer arithmetic, (i.e. is limited to 16-bit integers), substitution of the two "commented" lines for the one directly following them will render the routine entirely floating point.

      FUNCTION RAN3(IDUM)
Returns a uniform random deviate between 0.0 and 1.0. Set IDUM to any negative value
to initialize or reinitialize the sequence.
C         IMPLICIT REAL*4(M)
C         PARAMETER (MBIG=4000000.,MSEED=1618033.,MZ=0.,FAC=2.5E-7)
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.
DIMENSION MA(55)                  Save MA. This value is special and should not be modified; see Knuth
DATA IFF /0/
IF(IDUM.LT.0.OR.IFF.EQ.0)THEN     Initialization
IFF=1
MJ=MSEED-IABS(IDUM)             Initialize MA(55) using the seed IDUM and the large number MSEED.
MJ=MOD(MJ,MBIG)
MA(55)=MJ
MK=1
DO 11 I=1,54                    Now initialize the rest of the table
II=MOD(21*I,55)               in a slightly random order
MA(II)=MK                     with numbers that are not especially random
MK=MJ-MK
IF(MK.LT.MZ)MK=MK+MBIG
MJ=MA(II)
11      CONTINUE
DO 13 K=1,4                     We randomize them by "warming up the generator"
DO 12 I=1,55
MA(I)=MA(I)-MA(1+MOD(I+30,55))
IF(MA(I).LT.MZ)MA(I)=MA(I)+MBIG
12        CONTINUE
13      CONTINUE
INEXT=0                         Prepare indices for our first generated number
INEXTP=31                       The constant 31 is special; see Knuth
IDUM=1
ENDIF
INEXT=INEXT+1                     Here is where we start, except on initialization. Increment INEXT,
IF(INEXT.EQ.56)INEXT=1             wrapping around 56 to 1.
INEXTP=INEXTP+1                   Ditto for INEXTP
IF(INEXTP.EQ.56)INEXTP=1
MJ=MA(INEXT)-MA(INEXTP)           Now generate a new random number subtractively
IF(MJ.LT.MZ)MJ=MJ+MBIG            Be sure that it is in range
MA(INEXT)=MJ                      and output the derived uniform deviate
RAN3=MJ*FAC
RETURN
END

-
I suppose that the code of the random number generator just needs a seed. So, why not this one or $314159265$ ? – Claude Leibovici Jul 15 '14 at 9:52
– user11153 Jul 15 '14 at 14:08

It is rather common, when one needs a single "random" large constant that doesn't need to have any special properties (aside possibly from being too simple), to use digits from popular numbers. $\pi$ is the most common, I think, but the golden ratio rates as being a popular number so I would be unsurprised to see it used too.

-
Hand-waving a bit here, but: A reason for using digits from popular numbers as constants is to convince the users that the resulting numbers are "truly random", because using meaningless numbers as seeds could mean that the programmer knows what "random" numbers he wants and has figured out a random seed that gives those numbers. This is extremely important in cryptography, see e.g. en.wikipedia.org/wiki/Nothing_up_my_sleeve_number – JiK Jul 15 '14 at 13:29
@Jik, can you post your comment as an answer (plus perhaps some additional info)? – VividD Jul 15 '14 at 14:40
@JiK I believe your observation is the best answer to the dilemma from my question. Could you please formulate a full answer, along the same lines from your comment, I intend to mark it as the official answer to this question? – VividD Jul 20 '14 at 15:03

If you look at the code you find that the routine is from Numerical recipes. And if you look there you find the comment:According to Knuth, any large MBIG, and any smaller (but still large) MSEED can be substituted for the above values.

In fact the NR routine is derived from Knuth's subtractive generator IN55 (described in Seminumerical Algorithms 3.6), which also explains the magic 55 in the NR and MS code.

-

If you wanted to make a random number generator using just addition then the golden ratio is your best option. Check out "Additive Recurrence":

Low discrepancy sequence

Because it is an odd number it has full period (2^32 for int, 2^64 for long) with 2's complement arithmetic . For a proper random number generator you could then use a (invertable, to keep the full period) hash with that.
SplitMix64

-
Isn't the constant in question even rather than odd? The golden ratio itself is not an integer, hence neither even nor odd. And in any case the answer seems rather tangential to the question. – epimorphic Aug 15 '15 at 0:52