1 /*** This is a portable random number generator whose origins are
2 *** unknown. As far as can be told, this is public domain software.
5 /*** portable random number generator */
7 /*** Note that every variable used here must have at least 31 bits
8 *** of precision, exclusive of sign. Long integers should be enough.
9 *** The generator is the congruential: i = 7**5 * i mod (2^31-1).
12 #define MULTIPLIER 16807
13 #define MODULUS 2147483647
15 static long saved_seed;
18 /*** set_random - initialize constants and seed */
27 /*** ng_random - generate a random integer in the interval [a,b] (b >= a >= 0) */
34 hi = MULTIPLIER * (saved_seed >> 16);
35 lo = MULTIPLIER * (saved_seed & 0xffff);
41 if ((saved_seed = (hi<<16) + lo) < 0)
42 saved_seed += MODULUS;
46 return a + saved_seed % (b - a + 1);