source: git/libpolys/misc/sirandom.c @ 93d9b5

fieker-DuValspielwiese
Last change on this file since 93d9b5 was 0d97a85, checked in by Hans Schoenemann <hannes@…>, 10 years ago
cfRandom: part 2
  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include "sirandom.h"
2
3/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
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 *  Using the following multiplier and modulus, we obtain a
15 *  generator which:
16 *
17 *        1)  Has a full period: 1 to 2^31 - 2.
18 *        2)  Is testably "random" (see the article).
19 *        3)  Has a known implementation by E. L. Schrage.
20 */
21
22
23#define  A        16807        /*  A "good" multiplier          */
24#define  M   2147483647        /*  Modulus: 2^31 - 1          */
25#define  Q       127773        /*  M / A                  */
26#define  R         2836        /*  M % A                  */
27
28
29int siSeed = 1;
30
31int siRandNext(int r)
32{
33  r = A * (r % Q) - R * (r / Q);
34
35  if ( r < 0 )
36    r += M;
37
38  return( r );
39}
40
41int siRand()
42{
43  siSeed=siRandNext(siSeed);
44  return siSeed;
45}
46int siRandPlus1(int r)
47{
48  return r+1;
49}
Note: See TracBrowser for help on using the repository browser.