README.md (7777B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Mersenne Twister 22 23 > A 32-bit [Mersenne Twister][mersenne-twister] pseudorandom number generator. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var mt19937 = require( '@stdlib/random/base/mt19937' ); 31 ``` 32 33 #### mt19937() 34 35 Returns a pseudorandom integer on the interval `[1, 4294967295]`. 36 37 ```javascript 38 var r = mt19937(); 39 // returns <number> 40 ``` 41 42 #### mt19937.normalized() 43 44 Returns a pseudorandom number on the interval `[0,1)` with 53-bit precision. 45 46 ```javascript 47 var r = mt19937.normalized(); 48 // returns <number> 49 ``` 50 51 #### mt19937.factory( \[options] ) 52 53 Returns a 32-bit [Mersenne Twister][mersenne-twister] pseudorandom number generator. 54 55 ```javascript 56 var rand = mt19937.factory(); 57 ``` 58 59 The function accepts the following `options`: 60 61 - **seed**: pseudorandom number generator seed. 62 - **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. 63 - **copy**: `boolean` indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that a returned generator has exclusive control over its internal state. Default: `true`. 64 65 By default, a random integer is used to seed the returned generator. To seed the generator, provide either an `integer` on the interval `[1, 4294967295]` 66 67 ```javascript 68 var rand = mt19937.factory({ 69 'seed': 1234 70 }); 71 72 var r = rand(); 73 // returns 822569775 74 ``` 75 76 or, for arbitrary length seeds, an array-like `object` containing unsigned 32-bit integers 77 78 ```javascript 79 var Uint32Array = require( '@stdlib/array/uint32' ); 80 81 var rand = mt19937.factory({ 82 'seed': new Uint32Array( [ 291, 564, 837, 1110 ] ) 83 }); 84 85 var r = rand(); 86 // returns 1067595299 87 ``` 88 89 To return a generator having a specific initial state, set the generator `state` option. 90 91 ```javascript 92 var rand; 93 var bool; 94 var r; 95 var i; 96 97 // Generate pseudorandom numbers, thus progressing the generator state: 98 for ( i = 0; i < 1000; i++ ) { 99 r = mt19937(); 100 } 101 102 // Create a new MT19937 PRNG initialized to the current state of `mt19937`: 103 rand = mt19937.factory({ 104 'state': mt19937.state 105 }); 106 107 // Test that the generated pseudorandom numbers are the same: 108 bool = ( rand() === mt19937() ); 109 // returns true 110 ``` 111 112 #### mt19937.NAME 113 114 The generator name. 115 116 ```javascript 117 var str = mt19937.NAME; 118 // returns 'mt19937' 119 ``` 120 121 #### mt19937.MIN 122 123 Minimum possible value. 124 125 ```javascript 126 var min = mt19937.MIN; 127 // returns 1 128 ``` 129 130 #### mt19937.MAX 131 132 Maximum possible value. 133 134 ```javascript 135 var max = mt19937.MAX; 136 // returns 4294967295 137 ``` 138 139 #### mt19937.seed 140 141 The value used to seed `mt19937()`. 142 143 ```javascript 144 var rand; 145 var r; 146 var i; 147 148 // Generate pseudorandom values... 149 for ( i = 0; i < 100; i++ ) { 150 r = mt19937(); 151 } 152 153 // Generate the same pseudorandom values... 154 rand = mt19937.factory({ 155 'seed': mt19937.seed 156 }); 157 for ( i = 0; i < 100; i++ ) { 158 r = rand(); 159 } 160 ``` 161 162 #### mt19937.seedLength 163 164 Length of generator seed. 165 166 ```javascript 167 var len = mt19937.seedLength; 168 // returns <number> 169 ``` 170 171 #### mt19937.state 172 173 Writable property for getting and setting the generator state. 174 175 ```javascript 176 var r = mt19937(); 177 // returns <number> 178 179 r = mt19937(); 180 // returns <number> 181 182 // ... 183 184 // Get a copy of the current state: 185 var state = mt19937.state; 186 // returns <Uint32Array> 187 188 r = mt19937(); 189 // returns <number> 190 191 r = mt19937(); 192 // returns <number> 193 194 // Reset the state: 195 mt19937.state = state; 196 197 // Replay the last two pseudorandom numbers: 198 r = mt19937(); 199 // returns <number> 200 201 r = mt19937(); 202 // returns <number> 203 204 // ... 205 ``` 206 207 #### mt19937.stateLength 208 209 Length of generator state. 210 211 ```javascript 212 var len = mt19937.stateLength; 213 // returns <number> 214 ``` 215 216 #### mt19937.byteLength 217 218 Size (in bytes) of generator state. 219 220 ```javascript 221 var sz = mt19937.byteLength; 222 // returns <number> 223 ``` 224 225 #### mt19937.toJSON() 226 227 Serializes the pseudorandom number generator as a JSON object. 228 229 ```javascript 230 var o = mt19937.toJSON(); 231 // returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } 232 ``` 233 234 </section> 235 236 <!-- /.usage --> 237 238 <section class="notes"> 239 240 ## Notes 241 242 - [Mersenne Twister][mersenne-twister] is **not** a cryptographically secure PRNG, as the PRNG is based on a linear recursion. Any pseudorandom number sequence generated by a linear recursion is **insecure**, due to the fact that one can predict future generated outputs by observing a sufficiently long subsequence of generated values. 243 - Compared to other PRNGs, [Mersenne Twister][mersenne-twister] has a large state size (`~2.5kB`). Because of the large state size, beware of increased memory consumption when using the `factory()` method to create many [Mersenne Twister][mersenne-twister] PRNGs. When appropriate (e.g., when external state mutation is not a concern), consider sharing PRNG state. 244 - A seed array of length `1` is considered **equivalent** to an integer seed equal to the lone seed array element and vice versa. 245 - If PRNG state is "shared" (meaning a state array was provided during PRNG creation and **not** copied) and one sets the generator state to a state array having a different length, the PRNG does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize PRNG output according to the new shared state array, the state array for **each** relevant PRNG must be **explicitly** set. 246 - If PRNG state is "shared" and one sets the generator state to a state array of the same length, the PRNG state is updated (along with the state of all other PRNGs sharing the PRNG's state array). 247 - The PRNG has a period of `2^19937 - 1`. 248 249 </section> 250 251 <!-- /.notes --> 252 253 <section class="examples"> 254 255 ## Examples 256 257 <!-- eslint no-undef: "error" --> 258 259 ```javascript 260 var mt19937 = require( '@stdlib/random/base/mt19937' ); 261 262 var seed; 263 var rand; 264 var i; 265 266 // Generate pseudorandom numbers... 267 for ( i = 0; i < 100; i++ ) { 268 console.log( mt19937() ); 269 } 270 271 // Create a new pseudorandom number generator... 272 seed = 1234; 273 rand = mt19937.factory({ 274 'seed': seed 275 }); 276 for ( i = 0; i < 100; i++ ) { 277 console.log( rand() ); 278 } 279 280 // Create another pseudorandom number generator using a previous seed... 281 rand = mt19937.factory({ 282 'seed': mt19937.seed 283 }); 284 for ( i = 0; i < 100; i++ ) { 285 console.log( rand() ); 286 } 287 ``` 288 289 </section> 290 291 <!-- /.examples --> 292 293 * * * 294 295 <section class="references"> 296 297 ## References 298 299 - 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]. 300 - Harase, Shin. 2017. "Conversion of Mersenne Twister to double-precision floating-point numbers." _ArXiv_ abs/1708.06018 (September). <https://arxiv.org/abs/1708.06018>. 301 302 </section> 303 304 <!-- /.references --> 305 306 <section class="links"> 307 308 [mersenne-twister]: https://en.wikipedia.org/wiki/Mersenne_Twister 309 310 [@matsumoto:1998a]: https://doi.org/10.1145/272991.272995 311 312 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32 313 314 </section> 315 316 <!-- /.links -->