README.md (10644B)
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 # Box-Muller Transform 22 23 > Standard normally distributed pseudorandom numbers using the [Box-Muller transform][box-muller]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var randn = require( '@stdlib/random/base/box-muller' ); 31 ``` 32 33 #### randn() 34 35 Returns a standard normally distributed pseudorandom number. 36 37 ```javascript 38 var r = randn(); 39 // returns <number> 40 ``` 41 42 #### randn.factory( \[options] ) 43 44 Returns a pseudorandom number generator (PRNG) for generating standard normally distributed pseudorandom numbers. 45 46 ```javascript 47 var rand = randn.factory(); 48 ``` 49 50 The function accepts the following `options`: 51 52 - **prng**: pseudorandom number generator for generating uniformly distributed pseudorandom numbers on the interval `[0,1)`. If provided, the function **ignores** both the `state` and `seed` options. In order to seed the returned pseudorandom number generator, one must seed the provided `prng` (assuming the provided `prng` is seedable). 53 - **seed**: pseudorandom number generator seed. 54 - **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. 55 - **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`. 56 57 To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option. 58 59 ```javascript 60 var minstd = require( '@stdlib/random/base/minstd' ); 61 62 var rand = randn.factory({ 63 'prng': minstd.normalized 64 }); 65 66 var r = rand(); 67 // returns <number> 68 ``` 69 70 To seed a pseudorandom number generator, set the `seed` option. 71 72 ```javascript 73 var rand1 = randn.factory({ 74 'seed': 12345 75 }); 76 77 var r1 = rand1(); 78 // returns <number> 79 80 var rand2 = randn.factory({ 81 'seed': 12345 82 }); 83 84 var r2 = rand2(); 85 // returns <number> 86 87 var bool = ( r1 === r2 ); 88 // returns true 89 ``` 90 91 To return a generator having a specific initial state, set the generator `state` option. 92 93 ```javascript 94 var rand; 95 var bool; 96 var r; 97 var i; 98 99 // Generate pseudorandom numbers, thus progressing the generator state: 100 for ( i = 0; i < 1000; i++ ) { 101 r = randn(); 102 } 103 104 // Create a new PRNG initialized to the current state of `randn`: 105 rand = randn.factory({ 106 'state': randn.state 107 }); 108 109 // Test that the generated pseudorandom numbers are the same: 110 bool = ( rand() === randn() ); 111 // returns true 112 ``` 113 114 #### randn.NAME 115 116 The generator name. 117 118 ```javascript 119 var str = randn.NAME; 120 // returns 'box-muller' 121 ``` 122 123 #### randn.PRNG 124 125 The underlying pseudorandom number generator for uniformly distributed numbers on the interval `[0,1)`. 126 127 ```javascript 128 var prng = randn.PRNG; 129 // returns <Function> 130 ``` 131 132 #### randn.MIN 133 134 Minimum possible value. 135 136 ```javascript 137 var min = randn.MIN; 138 // returns <number> 139 ``` 140 141 Note that this value is computed based on the minimum value of the underlying PRNG for uniformly distributed numbers. If the underlying PRNG does not have a `MIN` property, this value is `null`. 142 143 <!-- eslint-disable stdlib/no-builtin-math --> 144 145 ```javascript 146 var rand = randn.factory({ 147 'prng': Math.random 148 }); 149 150 var min = rand.MIN; 151 // returns null 152 ``` 153 154 #### randn.MAX 155 156 Maximum possible value. 157 158 ```javascript 159 var max = randn.MAX; 160 // returns <number> 161 ``` 162 163 Note that this value is computed based on the minimum value of the underlying PRNG for uniformly distributed numbers. If the underlying PRNG does not have a `MIN` property, this value is `null`. 164 165 <!-- eslint-disable stdlib/no-builtin-math --> 166 167 ```javascript 168 var rand = randn.factory({ 169 'prng': Math.random 170 }); 171 172 var max = rand.MAX; 173 // returns null 174 ``` 175 176 #### randn.seed 177 178 The value used to seed `randn()`. 179 180 ```javascript 181 var rand; 182 var r; 183 var i; 184 185 // Generate pseudorandom values... 186 for ( i = 0; i < 100; i++ ) { 187 r = randn(); 188 } 189 190 // Generate the same pseudorandom values... 191 rand = randn.factory({ 192 'seed': randn.seed 193 }); 194 for ( i = 0; i < 100; i++ ) { 195 r = rand(); 196 } 197 ``` 198 199 If provided a PRNG for uniformly distributed numbers, this value is `null`. 200 201 <!-- eslint-disable stdlib/no-builtin-math --> 202 203 ```javascript 204 var rand = randn.factory({ 205 'prng': Math.random 206 }); 207 208 var seed = rand.seed; 209 // returns null 210 ``` 211 212 #### randn.seedLength 213 214 Length of generator seed. 215 216 ```javascript 217 var len = randn.seedLength; 218 // returns <number> 219 ``` 220 221 If provided a PRNG for uniformly distributed numbers, this value is `null`. 222 223 <!-- eslint-disable stdlib/no-builtin-math --> 224 225 ```javascript 226 var rand = randn.factory({ 227 'prng': Math.random 228 }); 229 230 var len = rand.seedLength; 231 // returns null 232 ``` 233 234 #### randn.state 235 236 Writable property for getting and setting the generator state. 237 238 ```javascript 239 var r = randn(); 240 // returns <number> 241 242 r = randn(); 243 // returns <number> 244 245 // ... 246 247 // Get a copy of the current state: 248 var state = randn.state; 249 // returns <Uint32Array> 250 251 r = randn(); 252 // returns <number> 253 254 r = randn(); 255 // returns <number> 256 257 // Reset the state: 258 randn.state = state; 259 260 // Replay the last two pseudorandom numbers: 261 r = randn(); 262 // returns <number> 263 264 r = randn(); 265 // returns <number> 266 267 // ... 268 ``` 269 270 If provided a PRNG for uniformly distributed numbers, this value is `null`. 271 272 <!-- eslint-disable stdlib/no-builtin-math --> 273 274 ```javascript 275 var rand = randn.factory({ 276 'prng': Math.random 277 }); 278 279 var state = rand.state; 280 // returns null 281 ``` 282 283 #### randn.stateLength 284 285 Length of generator state. 286 287 ```javascript 288 var len = randn.stateLength; 289 // returns <number> 290 ``` 291 292 If provided a PRNG for uniformly distributed numbers, this value is `null`. 293 294 <!-- eslint-disable stdlib/no-builtin-math --> 295 296 ```javascript 297 var rand = randn.factory({ 298 'prng': Math.random 299 }); 300 301 var len = rand.stateLength; 302 // returns null 303 ``` 304 305 #### randn.byteLength 306 307 Size (in bytes) of generator state. 308 309 ```javascript 310 var sz = randn.byteLength; 311 // returns <number> 312 ``` 313 314 If provided a PRNG for uniformly distributed numbers, this value is `null`. 315 316 <!-- eslint-disable stdlib/no-builtin-math --> 317 318 ```javascript 319 var rand = randn.factory({ 320 'prng': Math.random 321 }); 322 323 var sz = rand.byteLength; 324 // returns null 325 ``` 326 327 #### randn.toJSON() 328 329 Serializes the pseudorandom number generator as a JSON object. 330 331 ```javascript 332 var o = randn.toJSON(); 333 // returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } 334 ``` 335 336 If provided a PRNG for uniformly distributed numbers, this method returns `null`. 337 338 <!-- eslint-disable stdlib/no-builtin-math --> 339 340 ```javascript 341 var rand = randn.factory({ 342 'prng': Math.random 343 }); 344 345 var o = rand.toJSON(); 346 // returns null 347 ``` 348 349 </section> 350 351 <!-- /.usage --> 352 353 <section class="notes"> 354 355 ## Notes 356 357 - The minimum and maximum values are dependent on the number of bits used by the underlying PRNG. For instance, if a PRNG uses `32` bits, the smallest non-zero uniformly distributed pseudorandom number that can be generated is `2**-32`. Accordingly, the algorithm would be unable to produce random variates more than `6.66` standard deviations from the mean. This corresponds to a `2.74 x 10**-11` loss due to tail truncation. 358 - 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. 359 - 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). 360 361 </section> 362 363 <!-- /.notes --> 364 365 <section class="examples"> 366 367 ## Examples 368 369 <!-- eslint no-undef: "error" --> 370 371 ```javascript 372 var randn = require( '@stdlib/random/base/box-muller' ); 373 374 var seed; 375 var rand; 376 var i; 377 378 // Generate pseudorandom numbers... 379 for ( i = 0; i < 100; i++ ) { 380 console.log( randn() ); 381 } 382 383 // Create a new pseudorandom number generator... 384 seed = 1234; 385 rand = randn.factory({ 386 'seed': seed 387 }); 388 for ( i = 0; i < 100; i++ ) { 389 console.log( rand() ); 390 } 391 392 // Create another pseudorandom number generator using a previous seed... 393 rand = randn.factory({ 394 'seed': randn.seed 395 }); 396 for ( i = 0; i < 100; i++ ) { 397 console.log( rand() ); 398 } 399 ``` 400 401 </section> 402 403 <!-- /.examples --> 404 405 * * * 406 407 <section class="references"> 408 409 ## References 410 411 - Box, G. E. P., and Mervin E. Muller. 1958. "A Note on the Generation of Random Normal Deviates." _The Annals of Mathematical Statistics_ 29 (2). The Institute of Mathematical Statistics: 610–11. doi:[10.1214/aoms/1177706645][@box:1958]. 412 - Bell, James R. 1968. "Algorithm 334: Normal Random Deviates." _Communications of the ACM_ 11 (7). New York, NY, USA: ACM: 498. doi:[10.1145/363397.363547][@bell:1968]. 413 - Knop, R. 1969. "Remark on Algorithm 334 \[G5]: Normal Random Deviates." _Communications of the ACM_ 12 (5). New York, NY, USA: ACM: 281. doi:[10.1145/362946.362996][@knop:1969]. 414 - Marsaglia, G., and T. A. Bray. 1964. "A Convenient Method for Generating Normal Variables." _SIAM Review_ 6 (3). Society for Industrial; Applied Mathematics: 260–64. doi:[10.1137/1006063][@marsaglia:1964a]. 415 - Thomas, David B., Wayne Luk, Philip H.W. Leong, and John D. Villasenor. 2007. "Gaussian Random Number Generators." _ACM Computing Surveys_ 39 (4). New York, NY, USA: ACM. doi:[10.1145/1287620.1287622][@thomas:2007]. 416 417 </section> 418 419 <!-- /.references --> 420 421 <section class="links"> 422 423 [box-muller]: https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform 424 425 [@box:1958]: http://dx.doi.org/10.1214/aoms/1177706645 426 427 [@bell:1968]: http://dx.doi.org/10.1145/363397.363547 428 429 [@knop:1969]: http://dx.doi.org/10.1145/362946.362996 430 431 [@marsaglia:1964a]: http://dx.doi.org/10.1137/1006063 432 433 [@thomas:2007]: http://dx.doi.org/10.1145/1287620.1287622 434 435 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32 436 437 </section> 438 439 <!-- /.links -->