109 lines
3.2 KiB
JavaScript
109 lines
3.2 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_.
|
|||
|
*
|
|||
|
* This implementation subsequently shuffles the output of a linear congruential pseudorandom number generator (LCG) using a shuffle table in accordance with the Bays-Durham algorithm.
|
|||
|
*
|
|||
|
*
|
|||
|
* ## Notes
|
|||
|
*
|
|||
|
* - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279).
|
|||
|
*
|
|||
|
*
|
|||
|
* ## References
|
|||
|
*
|
|||
|
* - Bays, Carter, and S. D. Durham. 1976. "Improving a Poor Random Number Generator." _ACM Transactions on Mathematical Software_ 2 (1). New York, NY, USA: ACM: 59–64. doi:[10.1145/355666.355670](http://dx.doi.org/10.1145/355666.355670).
|
|||
|
* - Herzog, T.N., and G. Lord. 2002. _Applications of Monte Carlo Methods to Finance and Insurance_. ACTEX Publications. [https://books.google.com/books?id=vC7I\\\_gdX-A0C](https://books.google.com/books?id=vC7I\_gdX-A0C).
|
|||
|
* - 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;
|