source: trunk/minix/lib/other/lrand.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.1 KB
Line 
1/* lrand(3)
2 *
3 * Author: Terrence W. Holm Nov. 1988
4 *
5 *
6 * A prime modulus multiplicative linear congruential
7 * generator (PMMLCG), or "Lehmer generator".
8 * Implementation directly derived from the article:
9 *
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.
13 *
14 *
15 * Using the following multiplier and modulus, we obtain a
16 * generator which:
17 *
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.
21 */
22
23#include <lib.h>
24
25_PROTOTYPE( long seed, (long lseed));
26_PROTOTYPE( long lrand, (void));
27
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 */
32
33PRIVATE long _lseed = 1L;
34
35long seed(lseed)
36long lseed;
37{
38 long previous_seed = _lseed;
39
40 _lseed = lseed;
41
42 return(previous_seed);
43}
44
45
46long lrand()
47{
48 _lseed = A * (_lseed % Q) - R * (_lseed / Q);
49
50 if (_lseed < 0) _lseed += M;
51
52 return(_lseed);
53}
Note: See TracBrowser for help on using the repository browser.