at_wini also needs a pci_reserve() for the pci compatability device, if
[minix3.git] / lib / other / lrand.c
blobff475db5c98a95d59a31bde4bddb7fa17fa5662f
1 /* lrand(3)
3 * Author: Terrence W. Holm Nov. 1988
6 * A prime modulus multiplicative linear congruential
7 * generator (PMMLCG), or "Lehmer generator".
8 * Implementation directly derived from the article:
10 * S. K. Park and K. W. Miller
11 * Random Number Generators: Good Ones are Hard to Find
12 * CACM vol 31, #10. Oct. 1988. pp 1192-1201.
15 * Using the following multiplier and modulus, we obtain a
16 * generator which:
18 * 1) Has a full period: 1 to 2^31 - 2.
19 * 2) Is testably "random" (see the article).
20 * 3) Has a known implementation by E. L. Schrage.
23 #include <lib.h>
25 _PROTOTYPE( long seed, (long lseed));
26 _PROTOTYPE( long lrand, (void));
28 #define A 16807L /* A "good" multiplier */
29 #define M 2147483647L /* Modulus: 2^31 - 1 */
30 #define Q 127773L /* M / A */
31 #define R 2836L /* M % A */
33 PRIVATE long _lseed = 1L;
35 long seed(lseed)
36 long lseed;
38 long previous_seed = _lseed;
40 _lseed = lseed;
42 return(previous_seed);
46 long lrand()
48 _lseed = A * (_lseed % Q) - R * (_lseed / Q);
50 if (_lseed < 0) _lseed += M;
52 return(_lseed);