README.md (7341B)
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 # randn 22 23 > Create an iterator for generating pseudorandom numbers drawn from a standard normal distribution. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var iterator = require( '@stdlib/random/iter/randn' ); 31 ``` 32 33 #### iterator( \[options] ) 34 35 Returns an iterator for generating pseudorandom numbers drawn from a standard normal distribution. 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 - **name**: name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers. The following PRNGs are supported: 56 57 - [`improved-ziggurat`][@stdlib/random/base/improved-ziggurat]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/improved-ziggurat 58 - [`box-muller`][@stdlib/random/base/box-muller]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/box-muller 59 60 Default: [`'improved-ziggurat'`][@stdlib/random/base/improved-ziggurat]. 61 62 - **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). 63 64 - **seed**: pseudorandom number generator seed. 65 66 - **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. 67 68 - **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`. 69 70 - **iter**: number of iterations. 71 72 By default, the underlying pseudorandom number generator is [`improved-ziggurat`][@stdlib/random/base/improved-ziggurat]. To use a different PRNG, set the `name` option. 73 74 ```javascript 75 var it = iterator({ 76 'name': 'box-muller' 77 }); 78 79 var r = it.next().value; 80 // returns <number> 81 ``` 82 83 To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option. 84 85 ```javascript 86 var minstd = require( '@stdlib/random/base/minstd' ); 87 88 var it = iterator({ 89 'prng': minstd.normalized 90 }); 91 92 var r = it.next().value; 93 // returns <number> 94 ``` 95 96 To return an iterator having a specific initial state, set the iterator `state` option. 97 98 ```javascript 99 var bool; 100 var it1; 101 var it2; 102 var r; 103 var i; 104 105 it1 = iterator(); 106 107 // Generate pseudorandom numbers, thus progressing the generator state: 108 for ( i = 0; i < 1000; i++ ) { 109 r = it1.next().value; 110 } 111 112 // Create a new iterator initialized to the current state of `it1`: 113 it2 = iterator({ 114 'state': it1.state 115 }); 116 117 // Test that the generated pseudorandom numbers are the same: 118 bool = ( it1.next().value === it2.next().value ); 119 // returns true 120 ``` 121 122 To seed the iterator, set the `seed` option. 123 124 ```javascript 125 var it1 = iterator({ 126 'seed': 7823 127 }); 128 129 var r1 = it1.next().value; 130 // returns <number> 131 132 var it2 = iterator({ 133 'seed': 7823 134 }); 135 136 var r2 = it2.next().value; 137 // returns <number> 138 139 var bool = ( r1 === r2 ); 140 // returns true 141 ``` 142 143 To limit the number of iterations, set the `iter` option. 144 145 ```javascript 146 var it = iterator({ 147 'iter': 2 148 }); 149 150 var r = it.next().value; 151 // returns <number> 152 153 r = it.next().value; 154 // returns <number> 155 156 r = it.next().done; 157 // returns true 158 ``` 159 160 The returned iterator protocol-compliant object has the following properties: 161 162 - **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. 163 - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. 164 - **seed**: pseudorandom number generator seed. If provided a `prng` option, the property value is `null`. 165 - **seedLength**: length of generator seed. If provided a `prng` option, the property value is `null`. 166 - **state**: writable property for getting and setting the generator state. If provided a `prng` option, the property value is `null`. 167 - **stateLength**: length of generator state. If provided a `prng` option, the property value is `null`. 168 - **byteLength**: size (in bytes) of generator state. If provided a `prng` option, the property value is `null`. 169 - **PRNG**: underlying pseudorandom number generator. 170 171 </section> 172 173 <!-- /.usage --> 174 175 <section class="notes"> 176 177 ## Notes 178 179 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 180 - **Warning**: the default underlying source of pseudorandom numbers may **change** in the future. If exact reproducibility is required, either explicitly specify a PRNG via the `name` option or use an underlying PRNG directly. 181 - 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. 182 - 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). 183 184 </section> 185 186 <!-- /.notes --> 187 188 <section class="examples"> 189 190 ## Examples 191 192 <!-- eslint no-undef: "error" --> 193 194 ```javascript 195 var iterator = require( '@stdlib/random/iter/randn' ); 196 197 var it; 198 var r; 199 200 // Create a seeded iterator for generating pseudorandom numbers: 201 it = iterator({ 202 'seed': 1234, 203 'iter': 10 204 }); 205 206 // Perform manual iteration... 207 while ( true ) { 208 r = it.next(); 209 if ( r.done ) { 210 break; 211 } 212 console.log( r.value ); 213 } 214 ``` 215 216 </section> 217 218 <!-- /.examples --> 219 220 <section class="links"> 221 222 [@stdlib/random/base/improved-ziggurat]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/improved-ziggurat 223 224 [@stdlib/random/base/box-muller]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/box-muller 225 226 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32 227 228 </section> 229 230 <!-- /.links -->