Loading...
Searching...
No Matches
sirandom.c
Go to the documentation of this file.
#include "misc/auxiliary.h"
#include "sirandom.h"
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
/*
*
* A prime modulus multiplicative linear congruential
* generator (PMMLCG), or "Lehmer generator".
* Implementation directly derived from the article:
*
* S. K. Park and K. W. Miller
* Random Number Generators: Good Ones are Hard to Find
* CACM vol 31, #10. Oct. 1988. pp 1192-1201.
*
* Using the following multiplier and modulus, we obtain a
* generator which:
*
* 1) Has a full period: 1 to 2^31 - 2.
* 2) Is testably "random" (see the article).
* 3) Has a known implementation by E. L. Schrage.
*/
#define A 16807 /* A "good" multiplier */
#define M 2147483647 /* Modulus: 2^31 - 1 */
#define Q 127773 /* M / A */
#define R 2836 /* M % A */
VAR int siSeed = 1;
int siRandNext(int r)
{
r = A * (r % Q) - R * (r / Q);
if ( r < 0 )
r += M;
return( r );
}
int siRand()
{
siSeed=siRandNext(siSeed);
return siSeed;
}
int siRandPlus1(int r)
{
return r+1;
}
auxiliary.h
All the auxiliary stuff.
VAR
#define VAR
Definition globaldefs.h:5
siRand
int siRand()
Definition sirandom.c:42
siRandNext
int siRandNext(int r)
Definition sirandom.c:32
R
#define R
Definition sirandom.c:27
siSeed
VAR int siSeed
Definition sirandom.c:30
A
#define A
Definition sirandom.c:24
siRandPlus1
int siRandPlus1(int r)
Definition sirandom.c:47
M
#define M
Definition sirandom.c:25
Q
#define Q
Definition sirandom.c:26
sirandom.h