“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)
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/
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
DO 13 K=1,4 We randomize them by "warming up the generator"
DO 12 I=1,55
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