106 lines
2.8 KiB
JavaScript
106 lines
2.8 KiB
JavaScript
/**
|
||
* @license Apache-2.0
|
||
*
|
||
* Copyright (c) 2018 The Stdlib Authors.
|
||
*
|
||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
* you may not use this file except in compliance with the License.
|
||
* You may obtain a copy of the License at
|
||
*
|
||
* http://www.apache.org/licenses/LICENSE-2.0
|
||
*
|
||
* Unless required by applicable law or agreed to in writing, software
|
||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
* See the License for the specific language governing permissions and
|
||
* limitations under the License.
|
||
*/
|
||
|
||
'use strict';
|
||
|
||
// MODULES //
|
||
|
||
var factory = require( './factory.js' );
|
||
var randint32 = require( './rand_int32.js' );
|
||
|
||
|
||
// MAIN //
|
||
|
||
/**
|
||
* Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\).
|
||
*
|
||
* ## Method
|
||
*
|
||
* Linear congruential generators (LCGs) use the recurrence relation
|
||
*
|
||
* ```tex
|
||
* X_{n+1} = ( a \cdot X_n + c ) \operatorname{mod}(m)
|
||
* ```
|
||
*
|
||
* where the modulus \\( m \\) is a prime number or power of a prime number and \\( a \\) is a primitive root modulo \\( m \\).
|
||
*
|
||
* <!-- <note> -->
|
||
*
|
||
* For an LCG to be a Lehmer RNG, the seed \\( X_0 \\) must be coprime to \\( m \\).
|
||
*
|
||
* <!-- </note> -->
|
||
*
|
||
* In this implementation, the constants \\( a \\), \\( c \\), and \\( m \\) have the values
|
||
*
|
||
* ```tex
|
||
* \begin{align*}
|
||
* a &= 7^5 = 16807 \\
|
||
* c &= 0 \\
|
||
* m &= 2^{31} - 1 = 2147483647
|
||
* \end{align*}
|
||
* ```
|
||
*
|
||
* <!-- <note> -->
|
||
*
|
||
* The constant \\( m \\) is a Mersenne prime (modulo \\(31\\)).
|
||
*
|
||
* <!-- </note> -->
|
||
*
|
||
* <!-- <note> -->
|
||
*
|
||
* The constant \\( a \\) is a primitive root (modulo \\(31\\)).
|
||
*
|
||
* <!-- </note> -->
|
||
*
|
||
* Accordingly, the maximum possible product is
|
||
*
|
||
* ```tex
|
||
* 16807 \cdot (m - 1) \approx 2^{46}
|
||
* ```
|
||
*
|
||
* The values for \\( a \\), \\( c \\), and \\( m \\) are taken from Park and Miller, "Random Number Generators: Good Ones Are Hard To Find". Park's and Miller's article is also the basis for a recipe in the second edition of _Numerical Recipes in C_.
|
||
*
|
||
*
|
||
* ## Notes
|
||
*
|
||
* - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279).
|
||
*
|
||
*
|
||
* ## 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](http://dx.doi.org/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.
|
||
*
|
||
*
|
||
* @function minstd
|
||
* @type {PRNG}
|
||
* @returns {PositiveInteger} pseudorandom integer
|
||
*
|
||
* @example
|
||
* var v = minstd();
|
||
* // returns <number>
|
||
*/
|
||
var minstd = factory({
|
||
'seed': randint32()
|
||
});
|
||
|
||
|
||
// EXPORTS //
|
||
|
||
module.exports = minstd;
|