|
SuanShu, a Java numerical and statistical library | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectcom.numericalmethod.suanshu.stats.random.pseudorandom.linear.Lehmer
public class Lehmer
Lehmer proposed a general linear congruential generator that generates pseudo-random numbers in [0, 1]. It has this form:
xi+1 = (a * xi + c) mod m
ui+1 = xi+1 / m
We take c to be 0 because Marsaglia shows that there is little additional generality when c ≠ 0.
However, there are restrictions placed on the selection of (a, m) and the seed.
For example,
m;
m is a prime number or a power of a prime number;
a is an element of high multiplicative order modulo m
This class is essentially doing what Random.next(int) is doing (for a specific pair a and m),
but it computes (ax mod m) in integer arithmetic without overflow under certain conditions.
In addition, this allows customerized multiplier and modulus.
This class is the most fundamental building block for all linear random number generation algorithms in this library.
| Field Summary | |
|---|---|
long |
a
the multiplier |
long |
m
the modulus |
long |
q
m / a |
long |
r
m % a |
| Constructor Summary | |
|---|---|
Lehmer()
Construct a Lehmer (pure) linear congruential generator using default values. |
|
Lehmer(long a,
long m,
long seed)
Construct a Lehmer (pure) linear congruential generator. |
|
Lehmer(long a,
long m,
long k,
long seed)
Construct a skipping ahead Lehmer (pure) linear congruential generator. |
|
| Method Summary | |
|---|---|
long |
modulus()
Get the modulus of this linear congruential generator. |
double |
nextDouble()
Get the next random double between 0 and 1. |
long |
nextLong()
All built-in linear random number generators in this library ultimately call this function to generate random numbers. |
int |
order()
Get the order of recursion. |
void |
seed(long... seeds)
Seed the random number generator to produce repeatable sequences. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public final long a
public final long m
public final long q
public final long r
| Constructor Detail |
|---|
public Lehmer(long a,
long m,
long seed)
Suggested values are:
Note that a cannot be too big.
a - multiplierm - modulusseed - the seed. It should not be zero.
public Lehmer(long a,
long m,
long k,
long seed)
k value.
Equivalently, this call is the same as
Lehmer((a^k)%m, m, seed)
We compute (a^k)%m more efficiently.
Note that a cannot be too big.
a - multiplierm - modulusk - exponentseed - the seed. It should not be zero.public Lehmer()
| Method Detail |
|---|
public void seed(long... seeds)
RandomNumberGenerator
seed in interface RandomNumberGeneratorseeds - the seedspublic int order()
LinearCongruentialGenerator
order in interface LinearCongruentialGeneratorpublic long modulus()
LinearCongruentialGenerator
modulus in interface LinearCongruentialGeneratormpublic long nextLong()
If you are to write your own rng, you should either call this function, or have your own synchronization mechanism.
nextLong in interface RandomLongGeneratorpublic double nextDouble()
nextDouble in interface RandomNumberGenerator
|
SuanShu, a Java numerical and statistical library | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||