main.js (2863B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var factory = require( './factory.js' ); 24 var randint32 = require( './rand_int32.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\). 31 * 32 * ## Method 33 * 34 * Linear congruential generators (LCGs) use the recurrence relation 35 * 36 * ```tex 37 * X_{n+1} = ( a \cdot X_n + c ) \operatorname{mod}(m) 38 * ``` 39 * 40 * where the modulus \\( m \\) is a prime number or power of a prime number and \\( a \\) is a primitive root modulo \\( m \\). 41 * 42 * <!-- <note> --> 43 * 44 * For an LCG to be a Lehmer RNG, the seed \\( X_0 \\) must be coprime to \\( m \\). 45 * 46 * <!-- </note> --> 47 * 48 * In this implementation, the constants \\( a \\), \\( c \\), and \\( m \\) have the values 49 * 50 * ```tex 51 * \begin{align*} 52 * a &= 7^5 = 16807 \\ 53 * c &= 0 \\ 54 * m &= 2^{31} - 1 = 2147483647 55 * \end{align*} 56 * ``` 57 * 58 * <!-- <note> --> 59 * 60 * The constant \\( m \\) is a Mersenne prime (modulo \\(31\\)). 61 * 62 * <!-- </note> --> 63 * 64 * <!-- <note> --> 65 * 66 * The constant \\( a \\) is a primitive root (modulo \\(31\\)). 67 * 68 * <!-- </note> --> 69 * 70 * Accordingly, the maximum possible product is 71 * 72 * ```tex 73 * 16807 \cdot (m - 1) \approx 2^{46} 74 * ``` 75 * 76 * 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_. 77 * 78 * 79 * ## Notes 80 * 81 * - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279). 82 * 83 * 84 * ## References 85 * 86 * - 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). 87 * - 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. 88 * 89 * 90 * @function minstd 91 * @type {PRNG} 92 * @returns {PositiveInteger} pseudorandom integer 93 * 94 * @example 95 * var v = minstd(); 96 * // returns <number> 97 */ 98 var minstd = factory({ 99 'seed': randint32() 100 }); 101 102 103 // EXPORTS // 104 105 module.exports = minstd;