Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

  private const int MSEED = 161803398;

which reminds on 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.


After some research, I found that the most likely origin of the code I linked to at the top is 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.

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
        MJ=MSEED-IABS(IDUM)             Initialize MA(55) using the seed IDUM and the large number MSEED.
        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
11      CONTINUE
        DO 13 K=1,4                     We randomize them by "warming up the generator"
          DO 12 I=1,55
12        CONTINUE
13      CONTINUE
        INEXT=0                         Prepare indices for our first generated number
        INEXTP=31                       The constant 31 is special; see Knuth
      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
      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
share|improve this question
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

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.