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