README.md (7061B)
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 # MINSTD Shuffle 22 23 > Create an iterator for a linear congruential pseudorandom number generator ([LCG][lcg]) whose output is shuffled. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var iterator = require( '@stdlib/random/iter/minstd-shuffle' ); 31 ``` 32 33 #### iterator( \[options] ) 34 35 Returns an iterator for generating pseudorandom numbers via a linear congruential pseudorandom number generator ([LCG][lcg]) whose output is shuffled. 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 - **normalized**: `boolean` indicating whether to return pseudorandom numbers on the interval `[0,1)`. 56 - **seed**: pseudorandom number generator seed. 57 - **state**: an [`Int32Array`][@stdlib/array/int32] 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 return pseudorandom numbers on the interval `[0,1)`, set the `normalized` option. 62 63 ```javascript 64 var it = iterator({ 65 'normalized': true 66 }); 67 68 var r = it.next().value; 69 // returns <number> 70 ``` 71 72 To return an iterator having a specific initial state, set the iterator `state` option. 73 74 ```javascript 75 var bool; 76 var it1; 77 var it2; 78 var r; 79 var i; 80 81 it1 = iterator(); 82 83 // Generate pseudorandom numbers, thus progressing the generator state: 84 for ( i = 0; i < 1000; i++ ) { 85 r = it1.next().value; 86 } 87 88 // Create a new iterator initialized to the current state of `it1`: 89 it2 = iterator({ 90 'state': it1.state 91 }); 92 93 // Test that the generated pseudorandom numbers are the same: 94 bool = ( it1.next().value === it2.next().value ); 95 // returns true 96 ``` 97 98 To seed the iterator, set the `seed` option. 99 100 ```javascript 101 var it = iterator({ 102 'seed': 12345 103 }); 104 105 var r = it.next().value; 106 // returns 1982386332 107 108 it = iterator({ 109 'seed': 12345 110 }); 111 112 r = it.next().value; 113 // returns 1982386332 114 ``` 115 116 To limit the number of iterations, set the `iter` option. 117 118 ```javascript 119 var it = iterator({ 120 'iter': 2 121 }); 122 123 var r = it.next().value; 124 // returns <number> 125 126 r = it.next().value; 127 // returns <number> 128 129 r = it.next().done; 130 // returns true 131 ``` 132 133 The returned iterator protocol-compliant object has the following properties: 134 135 - **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. 136 - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. 137 - **seed**: pseudorandom number generator seed. 138 - **seedLength**: length of generator seed. 139 - **state**: writable property for getting and setting the generator state. 140 - **stateLength**: length of generator state. 141 - **byteLength**: size (in bytes) of generator state. 142 143 </section> 144 145 <!-- /.usage --> 146 147 <section class="notes"> 148 149 ## Notes 150 151 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 152 - The generator has a period of approximately `2.1e9` (see [Numerical Recipes in C, 2nd Edition](#references), p. 279). 153 - An [LCG][lcg] is fast and uses little memory. On the other hand, because the generator is a simple [linear congruential generator][lcg], the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. In general, this generator is unsuitable for Monte Carlo simulations and cryptographic applications. 154 - 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. 155 - 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). 156 157 </section> 158 159 <!-- /.notes --> 160 161 <section class="examples"> 162 163 ## Examples 164 165 <!-- eslint no-undef: "error" --> 166 167 ```javascript 168 var iterator = require( '@stdlib/random/iter/minstd-shuffle' ); 169 170 var it; 171 var r; 172 173 // Create a seeded iterator for generating pseudorandom numbers: 174 it = iterator({ 175 'seed': 1234, 176 'iter': 10 177 }); 178 179 // Perform manual iteration... 180 while ( true ) { 181 r = it.next(); 182 if ( r.done ) { 183 break; 184 } 185 console.log( r.value ); 186 } 187 ``` 188 189 </section> 190 191 <!-- /.examples --> 192 193 * * * 194 195 <section class="references"> 196 197 ## References 198 199 - Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042][@park:1988]. 200 - Bays, Carter, and S. D. Durham. 1976. "Improving a Poor Random Number Generator." _ACM Transactions on Mathematical Software_ 2 (1). New York, NY, USA: ACM: 59–64. doi:[10.1145/355666.355670][@bays:1976]. 201 - Herzog, T.N., and G. Lord. 2002. _Applications of Monte Carlo Methods to Finance and Insurance_. ACTEX Publications. [https://books.google.com/books?id=vC7I\\\_gdX-A0C][@herzog:2002]. 202 - Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press. 203 204 </section> 205 206 <!-- /.references --> 207 208 <section class="links"> 209 210 [lcg]: https://en.wikipedia.org/wiki/Linear_congruential_generator 211 212 [@park:1988]: http://dx.doi.org/10.1145/63039.63042 213 214 [@bays:1976]: http://dx.doi.org/10.1145/355666.355670 215 216 [@herzog:2002]: https://books.google.com/books?id=vC7I_gdX-A0C 217 218 [@stdlib/array/int32]: https://www.npmjs.com/package/@stdlib/array-int32 219 220 </section> 221 222 <!-- /.links -->