A linear congruential pseudorandom number generator (LCG) based on Park and Miller.
var minstd = require( '@stdlib/random/base/minstd' );
Returns a pseudorandom integer on the interval [1, 2147483646]
.
var v = minstd();
// returns <number>
Returns a pseudorandom number on the interval [0,1)
.
var v = minstd.normalized();
// returns <number>
Returns a linear congruential pseudorandom number generator (LCG).
var rand = minstd.factory();
The function accepts the following options
:
- seed: pseudorandom number generator seed.
By default, a random integer is used to seed the returned generator. To seed the generator, provide an integer
on the interval [1, 2147483646]
.
var rand = minstd.factory({
'seed': 1234
});
var v = rand();
// returns 20739838
The generator name.
var str = minstd.NAME;
// returns 'minstd'
The value used to seed minstd()
.
var rand;
var v;
var i;
// Generate pseudorandom values...
for ( i = 0; i < 100; i++ ) {
v = minstd();
}
// Generate the same pseudorandom values...
rand = minstd.factory({
'seed': minstd.SEED
});
for ( i = 0; i < 100; i++ ) {
v = rand();
}
Minimum possible value.
var min = minstd.MIN;
// returns 1
Maximum possible value.
var max = minstd.MAX;
// returns 2147483646
- The generator has a period of approximately
2.1e9
(see Numerical Recipes in C, 2nd Edition, p. 279). - An LCG is fast and uses little memory. On the other hand, because the generator is a simple linear congruential generator, the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. For more on the advantages and disadvantages of LCGs, see Wikipedia.
var minstd = require( '@stdlib/random/base/minstd' );
var seed;
var rand;
var i;
// Generate pseudorandom numbers...
for ( i = 0; i < 100; i++ ) {
console.log( minstd() );
}
// Create a new pseudorandom number generator...
seed = 1234;
rand = minstd.factory({
'seed': seed
});
for ( i = 0; i < 100; i++ ) {
console.log( rand() );
}
// Create another pseudorandom number generator using a previous seed...
rand = minstd.factory({
'seed': minstd.SEED
});
for ( i = 0; i < 100; i++ ) {
console.log( rand() );
}
- Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." Communications of the ACM 31 (10). New York, NY, USA: ACM: 1192–1201. doi:10.1145/63039.63042.
- Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. Numerical Recipes in C: The Art of Scientific Computing, Second Edition. Cambridge University Press.