main.js (3277B)
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 * 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. 79 * 80 * 81 * ## Notes 82 * 83 * - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279). 84 * 85 * 86 * ## References 87 * 88 * - 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). 89 * - 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). 90 * - 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. 91 * 92 * 93 * @function minstd 94 * @type {PRNG} 95 * @returns {PositiveInteger} pseudorandom integer 96 * 97 * @example 98 * var v = minstd(); 99 * // returns <number> 100 */ 101 var minstd = factory({ 102 'seed': randint32() 103 }); 104 105 106 // EXPORTS // 107 108 module.exports = minstd;