#include <stdio.h> #define A1 7141 #define C1 54773 #define M1 259200 #define RM1 (1.0 / M1) #define A2 8121 #define C2 28411 #define M2 134456 #define RM2 (1.0 / M2) #define A3 4561 #define C3 51349 #define M3 243000 #define MASK 131071 float uran3( int *x ) /*--------------------------------------------------------------------- * This routine generates uniformly distributed random numbers in the * range [0, 1) using the linear congruential method. * Actually, 3 separate generators are used, generating the upper and * lower halves of the output and controlling a shuffling routine. * * Notes: * 1. A negative seed value initializes the sequence. The returned * seed is 1. * 2. The values A,C, and M are taken from _Numerical Recipes in C_, * p.211 and are claimed to yield maximal length sequences of period * M and to pass Knuth's spectral test for dimensions 2, 3, 4, 5 & 6. *--------------------------------------------------------------------- */ { static long ix1, ix2, ix3; static float r[98]; static int iff = 0; float temp; int j; /* init on first call or when *x is negative */ if ( *x < 0 || iff == 0 ) { iff = 1; ix1 = (C1 - *x) % M1; ix1 = (A1 * ix1 + C1) % M1; ix2 = ix1 % M2; ix1 = (A1 * ix1 + C1) % M1; ix3 = ix1 % M3; for ( j = 1; j < 97; j++ ) { ix1 = (A1 * ix1 + C1) % M1; ix2 = (A2 * ix2 + C2) % M2; r[j] = (ix1 + ix2 * RM2) * RM1; } *x = 1; } /* normal operation */ ix1 = (A1 * ix1 + C1) % M1; ix2 = (A2 * ix2 + C2) % M2; ix3 = (A3 * ix3 + C3) % M3; j = 1 + ((97 * ix3) / M3); temp = r[j]; r[j] = (ix1 + ix2 * RM2) * RM1; return( temp ); }
So I created the following global logic RANDOM (type Numeric)
PARMS(#X) DEFINE #r(98,10.20) #A1 = 7141 #C1 = 54773 #M1 = 259200 #RM1 = (1 / #M1) #A2 = 8121 #C2 = 28411 #M2 = 134456 #RM2 = (1 / #M2) #A3 = 4561 #C3 = 51349 #M3 = 243000 #MASK = 131071 #iff = 0 IF ( #X < 0 OR #iff = 0 ) THEN #iff = 1 #ix1 = REM((#C1 - #X),#M1 ) #ix1 = REM((#A1 * #ix1 + #C1),#M1) #ix2 = REM(#ix1,#M2) #ix1 = REM((#A1 * #ix1 + #C1),#M1) #ix3 = REM(#ix1,#M3) FOR #j = 1 TO 97 #ix1 = REM((#A1 * #ix1 + #C1),#M1) #ix2 = REM((#A2 * #ix2 + #C2),#M2) #r(#j) = (#ix1 + #ix2 * #RM2) * #RM1 ENDFOR #X = 1 ENDIF #ix1 = REM((#A1 * #ix1 + #C1),#M1) #ix2 = REM((#A2 * #ix2 + #C2),#M2) #ix3 = REM((#A3 * #ix3 + #C3),#M3) #j = 1 + ((97 * #ix3) / #M3) #temp = #r(#j) #r(#j) = (#ix1 + #ix2 * #RM2) * #RM1 RETURN(#temp )
But when I call the global, I allways get the same answer back.
My C knowledge is very limited.
Can anyone tell me how the global logic should be created?