README.md (7155B)
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 # Standard Normal Random Numbers 22 23 > Create an iterator for generating pseudorandom numbers drawn from a [standard normal][normal] distribution using the [Improved Ziggurat][ziggurat-algorithm] algorithm. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var iterator = require( '@stdlib/random/iter/improved-ziggurat' ); 31 ``` 32 33 #### iterator( \[options] ) 34 35 Returns an iterator for generating pseudorandom numbers drawn from a [standard normal][normal] distribution using the [Improved Ziggurat][ziggurat-algorithm] algorithm. 36 37 ```javascript 38 var it = iterator(); 39 // returns <Object> 40 41 var r = it.next().value; 42 // returns <number> 43 44 r = it.next().value; 45 // returns <number> 46 47 r = it.next().value; 48 // returns <number> 49 50 // ... 51 ``` 52 53 The function accepts the following `options`: 54 55 - **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 iterator, one must seed the provided `prng` (assuming the provided `prng` is seedable). 56 - **seed**: pseudorandom number generator seed. 57 - **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. 58 - **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 iterator has exclusive control over its internal pseudorandom number generator state. Default: `true`. 59 - **iter**: number of iterations. 60 61 To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option. 62 63 ```javascript 64 var minstd = require( '@stdlib/random/base/minstd' ); 65 66 var it = iterator({ 67 'prng': minstd.normalized 68 }); 69 70 var r = it.next().value; 71 // returns <number> 72 ``` 73 74 To return an iterator having a specific initial state, set the iterator `state` option. 75 76 ```javascript 77 var bool; 78 var it1; 79 var it2; 80 var r; 81 var i; 82 83 it1 = iterator(); 84 85 // Generate pseudorandom numbers, thus progressing the generator state: 86 for ( i = 0; i < 1000; i++ ) { 87 r = it1.next().value; 88 } 89 90 // Create a new iterator initialized to the current state of `it1`: 91 it2 = iterator({ 92 'state': it1.state 93 }); 94 95 // Test that the generated pseudorandom numbers are the same: 96 bool = ( it1.next().value === it2.next().value ); 97 // returns true 98 ``` 99 100 To seed the iterator, set the `seed` option. 101 102 ```javascript 103 var it = iterator({ 104 'seed': 12345 105 }); 106 107 var r = it.next().value; 108 // returns ~1.789 109 110 it = iterator({ 111 'seed': 12345 112 }); 113 114 r = it.next().value; 115 // returns ~1.789 116 ``` 117 118 To limit the number of iterations, set the `iter` option. 119 120 ```javascript 121 var it = iterator({ 122 'iter': 2 123 }); 124 125 var r = it.next().value; 126 // returns <number> 127 128 r = it.next().value; 129 // returns <number> 130 131 r = it.next().done; 132 // returns true 133 ``` 134 135 The returned iterator protocol-compliant object has the following properties: 136 137 - **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished. 138 - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. 139 - **seed**: pseudorandom number generator seed. If provided a `prng` option, the property value is `null`. 140 - **seedLength**: length of generator seed. If provided a `prng` option, the property value is `null`. 141 - **state**: writable property for getting and setting the generator state. If provided a `prng` option, the property value is `null`. 142 - **stateLength**: length of generator state. If provided a `prng` option, the property value is `null`. 143 - **byteLength**: size (in bytes) of generator state. If provided a `prng` option, the property value is `null`. 144 - **PRNG**: underlying pseudorandom number generator. 145 146 </section> 147 148 <!-- /.usage --> 149 150 <section class="notes"> 151 152 ## Notes 153 154 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 155 - If PRNG state is "shared" (meaning a state array was provided during iterator creation and **not** copied) and one sets the underlying generator state to a state array having a different length, the iterator does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize the output of the underlying generator according to the new shared state array, the state array for **each** relevant iterator and/or PRNG must be **explicitly** set. 156 - If PRNG state is "shared" and one sets the underlying generator state to a state array of the same length, the PRNG state is updated (along with the state of all other iterator and/or PRNGs sharing the PRNG's state array). 157 158 </section> 159 160 <!-- /.notes --> 161 162 <section class="examples"> 163 164 ## Examples 165 166 <!-- eslint no-undef: "error" --> 167 168 ```javascript 169 var iterator = require( '@stdlib/random/iter/improved-ziggurat' ); 170 171 var it; 172 var r; 173 174 // Create a seeded iterator for generating pseudorandom numbers: 175 it = iterator({ 176 'seed': 1234, 177 'iter': 10 178 }); 179 180 // Perform manual iteration... 181 while ( true ) { 182 r = it.next(); 183 if ( r.done ) { 184 break; 185 } 186 console.log( r.value ); 187 } 188 ``` 189 190 </section> 191 192 <!-- /.examples --> 193 194 * * * 195 196 <section class="references"> 197 198 ## References 199 200 - Doornik, Jurgen A. 2005. "An Improved Ziggurat Method to Generate Normal Random Samples." [<https://www.doornik.com/research/ziggurat.pdf>][@doornik:2005]. 201 - Marsaglia, George, and Wai Wan Tsang. 2000. "The Ziggurat Method for Generating Random Variables." _Journal of Statistical Software_ 5 (1): 1–7. doi:[10.18637/jss.v005.i08][@marsaglia:2000b]. 202 - Marsaglia, George. 1964. "Generating a Variable from the Tail of the Normal Distribution." _Technometrics_ 6 (1): 101–2. doi:[10.1080/00401706.1964.10490150][@marsaglia:1964b]. 203 204 </section> 205 206 <!-- /.references --> 207 208 <section class="links"> 209 210 [ziggurat-algorithm]: https://en.wikipedia.org/wiki/Ziggurat_algorithm 211 212 [normal]: https://en.wikipedia.org/wiki/Normal_distribution 213 214 [@doornik:2005]: https://www.doornik.com/research/ziggurat.pdf 215 216 [@marsaglia:2000b]: http://dx.doi.org/10.18637/jss.v005.i08 217 218 [@marsaglia:1964b]: https://doi.org/10.1080/00401706.1964.10490150 219 220 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32 221 222 </section> 223 224 <!-- /.links -->