README.md (6317B)
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 # Arcsine Random Numbers 22 23 > Create an iterator for generating pseudorandom numbers drawn from an [arcsine][arcsine] distribution. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var iterator = require( '@stdlib/random/iter/arcsine' ); 31 ``` 32 33 #### iterator( a, b\[, options] ) 34 35 Returns an iterator for generating pseudorandom numbers drawn from an [arcsine][arcsine] distribution with parameters `a` (minimum support) and `b` (maximum support). 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 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( 2.0, 4.0, { 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( 2.0, 4.0 ); 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( 2.0, 4.0, { 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 it1 = iterator( 2.0, 4.0, { 104 'seed': 12345 105 }); 106 107 var r1 = it1.next().value; 108 // returns <number> 109 110 var it2 = iterator( 2.0, 4.0, { 111 'seed': 12345 112 }); 113 114 var r2 = it2.next().value; 115 // returns <number> 116 117 var bool = ( r1 === r2 ); 118 // returns true 119 ``` 120 121 To limit the number of iterations, set the `iter` option. 122 123 ```javascript 124 var it = iterator( 2.0, 4.0, { 125 'iter': 2 126 }); 127 128 var r = it.next().value; 129 // returns <number> 130 131 r = it.next().value; 132 // returns <number> 133 134 r = it.next().done; 135 // returns true 136 ``` 137 138 The returned iterator protocol-compliant object has the following properties: 139 140 - **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. 141 - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. 142 - **seed**: pseudorandom number generator seed. If provided a `prng` option, the property value is `null`. 143 - **seedLength**: length of generator seed. If provided a `prng` option, the property value is `null`. 144 - **state**: writable property for getting and setting the generator state. If provided a `prng` option, the property value is `null`. 145 - **stateLength**: length of generator state. If provided a `prng` option, the property value is `null`. 146 - **byteLength**: size (in bytes) of generator state. If provided a `prng` option, the property value is `null`. 147 - **PRNG**: underlying pseudorandom number generator. 148 149 </section> 150 151 <!-- /.usage --> 152 153 <section class="notes"> 154 155 ## Notes 156 157 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 158 - 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. 159 - 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). 160 161 </section> 162 163 <!-- /.notes --> 164 165 <section class="examples"> 166 167 ## Examples 168 169 <!-- eslint no-undef: "error" --> 170 171 ```javascript 172 var iterator = require( '@stdlib/random/iter/arcsine' ); 173 174 var it; 175 var r; 176 177 // Create a seeded iterator for generating pseudorandom numbers: 178 it = iterator( 2.0, 5.0, { 179 'seed': 1234, 180 'iter': 10 181 }); 182 183 // Perform manual iteration... 184 while ( true ) { 185 r = it.next(); 186 if ( r.done ) { 187 break; 188 } 189 console.log( r.value ); 190 } 191 ``` 192 193 </section> 194 195 <!-- /.examples --> 196 197 <section class="links"> 198 199 [arcsine]: https://en.wikipedia.org/wiki/Arcsine_distribution 200 201 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32 202 203 </section> 204 205 <!-- /.links -->