MINSTD

A linear congruential pseudorandom number generator (LCG) based on Park and Miller.

Usage

``````var minstd = require( '@stdlib/random/base/minstd' );
``````

minstd()

Returns a pseudorandom integer on the interval `[1, 2147483646]`.

``````var v = minstd();
// returns <number>
``````

minstd.normalized()

Returns a pseudorandom number on the interval `[0,1)`.

``````var v = minstd.normalized();
// returns <number>
``````

minstd.factory( [seed] )

Returns a linear congruential pseudorandom number generator (LCG).

``````var rand = minstd.factory();
``````

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( 1234 );

var v = rand();
// returns 20739838
``````

minstd.NAME

The generator name.

``````var str = minstd.NAME;
// returns 'minstd'
``````

minstd.SEED

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( minstd.SEED );
for ( i = 0; i < 100; i++ ) {
v = rand();
}
``````

minstd.MIN

Minimum possible value.

``````var min = minstd.MIN;
// returns 1
``````

minstd.MAX

Maximum possible value.

``````var max = minstd.MAX;
// returns 2147483646
``````

Notes

• 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.

Examples

``````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 );
for ( i = 0; i < 100; i++ ) {
console.log( rand() );
}

// Create another pseudorandom number generator using a previous seed...
rand = minstd.factory( minstd.SEED );
for ( i = 0; i < 100; i++ ) {
console.log( rand() );
}
``````

References

• 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.