README.md (6545B)
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 # randu 22 23 > Create an iterator for generating uniformly distributed pseudorandom numbers between `0` and `1`. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var iterator = require( '@stdlib/random/iter/randu' ); 31 ``` 32 33 #### iterator( \[options] ) 34 35 Returns an iterator for generating uniformly distributed pseudorandom numbers between `0` and `1`. 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 generators are supported: 56 57 - [`mt19937`][@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/mt19937 58 - [`minstd`][@stdlib/random/base/minstd]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd 59 - [`minstd-shuffle`][@stdlib/random/base/minstd-shuffle]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd-shuffle 60 61 Default: [`'mt19937'`][@stdlib/random/base/mt19937]. 62 63 - **seed**: pseudorandom number generator seed. Valid seed values vary according to the underlying PRNG. 64 65 - **state**: pseudorandom number generator state. Valid state values vary according to the underlying PRNG. If provided, the function ignores the `seed` option. 66 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 generator has exclusive control over its internal state. Default: `true`. 68 69 - **iter**: number of iterations. 70 71 By default, the underlying pseudorandom number generator is [`mt19937`][@stdlib/random/base/mt19937]. To use a different PRNG, set the `name` option. 72 73 ```javascript 74 var it = iterator({ 75 'name': 'minstd-shuffle' 76 }); 77 78 var r = it.next().value; 79 // returns <number> 80 ``` 81 82 To return an iterator having a specific initial state, set the iterator `state` option. 83 84 ```javascript 85 var bool; 86 var it1; 87 var it2; 88 var r; 89 var i; 90 91 it1 = iterator(); 92 93 // Generate pseudorandom numbers, thus progressing the generator state: 94 for ( i = 0; i < 1000; i++ ) { 95 r = it1.next().value; 96 } 97 98 // Create a new iterator initialized to the current state of `it1`: 99 it2 = iterator({ 100 'state': it1.state 101 }); 102 103 // Test that the generated pseudorandom numbers are the same: 104 bool = ( it1.next().value === it2.next().value ); 105 // returns true 106 ``` 107 108 To seed the iterator, set the `seed` option. 109 110 ```javascript 111 var it1 = iterator({ 112 'seed': 7823 113 }); 114 115 var r1 = it1.next().value; 116 // returns <number> 117 118 var it2 = iterator({ 119 'seed': 7823 120 }); 121 122 var r2 = it2.next().value; 123 // returns <number> 124 125 var bool = ( r1 === r2 ); 126 // returns true 127 ``` 128 129 To limit the number of iterations, set the `iter` option. 130 131 ```javascript 132 var it = iterator({ 133 'iter': 2 134 }); 135 136 var r = it.next().value; 137 // returns <number> 138 139 r = it.next().value; 140 // returns <number> 141 142 r = it.next().done; 143 // returns true 144 ``` 145 146 The returned iterator protocol-compliant object has the following properties: 147 148 - **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. 149 - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. 150 - **seed**: pseudorandom number generator seed. 151 - **seedLength**: length of generator seed. 152 - **state**: writable property for getting and setting the generator state. 153 - **stateLength**: length of generator state. 154 - **byteLength**: size (in bytes) of generator state. 155 - **PRNG**: underlying pseudorandom number generator. 156 157 </section> 158 159 <!-- /.usage --> 160 161 <section class="notes"> 162 163 ## Notes 164 165 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 166 - **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. 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/randu' ); 182 183 var it; 184 var r; 185 186 // Create a seeded iterator for generating pseudorandom numbers: 187 it = iterator({ 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 [@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/mt19937 209 210 [@stdlib/random/base/minstd]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd 211 212 [@stdlib/random/base/minstd-shuffle]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd-shuffle 213 214 </section> 215 216 <!-- /.links -->