main.js (4062B)
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 randuint32 = require( './rand_uint32.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Generates a pseudorandom integer on the interval \\( [1,2^{32}-1) \\). 31 * 32 * ## Method 33 * 34 * - When generating normalized double-precision floating-point numbers, we first generate two pseudorandom integers \\( x \\) and \\( y \\) on the interval \\( [1,2^{32}-1) \\) for a combined \\( 64 \\) random bits. 35 * 36 * - We would like \\( 53 \\) random bits to generate a 53-bit precision integer and, thus, want to discard \\( 11 \\) of the generated bits. 37 * 38 * - We do so by discarding \\( 5 \\) bits from \\( x \\) and \\( 6 \\) bits from \\( y \\). 39 * 40 * - Accordingly, \\( x \\) contains \\( 27 \\) random bits, which are subsequently shifted left \\( 26 \\) bits (multiplied by \\( 2^{26} \\), and \\( y \\) contains \\( 26 \\) random bits to fill in the lower \\( 26 \\) bits. When summed, they combine to comprise \\( 53 \\) random bits of a double-precision floating-point integer. 41 * 42 * - As an example, suppose, for the sake of argument, the 32-bit PRNG generates the maximum unsigned 32-bit integer \\( 2^{32}-1 \\) twice in a row. Then, 43 * 44 * ```javascript 45 * x = 4294967295 >>> 5; // 00000111111111111111111111111111 46 * y = 4294967295 >>> 6; // 00000011111111111111111111111111 47 * ``` 48 * 49 * Multiplying \\( x \\) by \\( 2^{26} \\) returns \\( 9007199187632128 \\), which, in binary, is 50 * 51 * ```binarystring 52 * 0 10000110011 11111111111111111111 11111100000000000000000000000000 53 * ``` 54 * 55 * Adding \\( y \\) yields \\( 9007199254740991 \\) (the maximum "safe" double-precision floating-point integer value), which, in binary, is 56 * 57 * ```binarystring 58 * 0 10000110011 11111111111111111111 11111111111111111111111111111111 59 * ``` 60 * 61 * - Similarly, suppose the 32-bit PRNG generates the following values 62 * 63 * ```javascript 64 * x = 1 >>> 5; // 0 => 00000000000000000000000000000000 65 * y = 64 >>> 6; // 1 => 00000000000000000000000000000001 66 * ``` 67 * 68 * Multiplying \\( x \\) by \\( 2^{26} \\) returns \\( 0 \\), which, in binary, is 69 * 70 * ```binarystring 71 * 0 00000000000 00000000000000000000 00000000000000000000000000000000 72 * ``` 73 * 74 * Adding \\( y \\) yields \\( 1 \\), which, in binary, is 75 * 76 * ```binarystring 77 * 0 01111111111 00000000000000000000 00000000000000000000000000000000 78 * ``` 79 * 80 * - As different combinations of \\( x \\) and \\( y \\) are generated, different combinations of double-precision floating-point exponent and significand bits will be toggled, thus generating pseudorandom double-precision floating-point numbers. 81 * 82 * 83 * ## References 84 * 85 * - Matsumoto, Makoto, and Takuji Nishimura. 1998. "Mersenne Twister: A 623-dimensionally Equidistributed Uniform Pseudo-random Number Generator." _ACM Transactions on Modeling and Computer Simulation_ 8 (1). New York, NY, USA: ACM: 3–30. doi:[10.1145/272991.272995][@matsumoto:1998a]. 86 * - Harase, Shin. 2017. "Conversion of Mersenne Twister to double-precision floating-point numbers." _ArXiv_ abs/1708.06018 (September). <https://arxiv.org/abs/1708.06018>. 87 * 88 * [@matsumoto:1998a]: https://doi.org/10.1145/272991.272995 89 * 90 * 91 * @function mt19937 92 * @type {PRNG} 93 * @returns {PositiveInteger} pseudorandom integer 94 * 95 * @example 96 * var v = mt19937(); 97 * // returns <number> 98 */ 99 var mt19937 = factory({ 100 'seed': randuint32() 101 }); 102 103 104 // EXPORTS // 105 106 module.exports = mt19937;