README.md (7219B)
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 # randi 22 23 > Pseudorandom numbers having integer values. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var randi = require( '@stdlib/random/base/randi' ); 31 ``` 32 33 #### randi() 34 35 Returns a pseudorandom number having an integer value. 36 37 ```javascript 38 var v = randi(); 39 // returns <number> 40 ``` 41 42 #### randi.factory( \[options] ) 43 44 Returns a pseudorandom number generator (PRNG) for generating random numbers. 45 46 ```javascript 47 var rand = randi.factory(); 48 ``` 49 50 The function accepts the following `options`: 51 52 - **name**: name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers. The following generators are supported: 53 54 - [`mt19937`][@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/mt19937 55 - [`minstd`][@stdlib/random/base/minstd]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd 56 - [`minstd-shuffle`][@stdlib/random/base/minstd-shuffle]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd-shuffle 57 58 Default: [`'mt19937'`][@stdlib/random/base/mt19937]. 59 60 - **seed**: pseudorandom number generator seed. Valid seed values vary according to the underlying PRNG. 61 62 - **state**: pseudorandom number generator state. Valid state values vary according to the underlying PRNG. If provided, the function ignores the `seed` option. 63 64 - **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`. 65 66 By default, the underlying pseudorandom number generator is [`mt19937`][@stdlib/random/base/mt19937]. To use a different PRNG, set the `name` option. 67 68 ```javascript 69 var rand = randi.factory({ 70 'name': 'minstd-shuffle' 71 }); 72 73 var v = rand(); 74 // returns <number> 75 ``` 76 77 To seed a pseudorandom number generator, set the `seed` option. 78 79 ```javascript 80 var rand1 = randi.factory({ 81 'seed': 12345 82 }); 83 84 var r1 = rand1(); 85 // returns <number> 86 87 var rand2 = randi.factory({ 88 'seed': 12345 89 }); 90 91 var r2 = rand2(); 92 // returns <number> 93 94 var bool = ( r1 === r2 ); 95 // returns true 96 ``` 97 98 To return a generator having a specific initial state, set the generator `state` option. 99 100 ```javascript 101 var rand; 102 var bool; 103 var r; 104 var i; 105 106 // Generate pseudorandom numbers, thus progressing the generator state: 107 for ( i = 0; i < 1000; i++ ) { 108 r = randi(); 109 } 110 111 // Create a new PRNG initialized to the current state of `randi`: 112 rand = randi.factory({ 113 'state': randi.state 114 }); 115 116 // Test that the generated pseudorandom numbers are the same: 117 bool = ( rand() === randi() ); 118 // returns true 119 ``` 120 121 #### randi.NAME 122 123 The generator name. 124 125 ```javascript 126 var str = randi.NAME; 127 // returns 'randi' 128 ``` 129 130 #### randi.PRNG 131 132 The underlying pseudorandom number generator. 133 134 ```javascript 135 var prng = randi.PRNG; 136 // returns <Function> 137 ``` 138 139 #### randi.MIN 140 141 Minimum value lower bound (specific to underlying PRNG). 142 143 ```javascript 144 var min = randi.MIN; 145 // returns <number> 146 ``` 147 148 #### randi.MAX 149 150 Maximum value upper bound (specific to underlying PRNG). 151 152 ```javascript 153 var max = randi.MAX; 154 // returns <number> 155 ``` 156 157 #### randi.seed 158 159 The value used to seed `randi()`. 160 161 ```javascript 162 var rand; 163 var v; 164 var i; 165 166 // Generate pseudorandom values... 167 for ( i = 0; i < 100; i++ ) { 168 v = randi(); 169 } 170 171 // Generate the same pseudorandom values... 172 rand = randi.factory({ 173 'seed': randi.seed 174 }); 175 for ( i = 0; i < 100; i++ ) { 176 v = rand(); 177 } 178 ``` 179 180 #### randi.seedLength 181 182 Length of generator seed. 183 184 ```javascript 185 var len = randi.seedLength; 186 // returns <number> 187 ``` 188 189 #### randi.state 190 191 Writable property for getting and setting the generator state. 192 193 ```javascript 194 var r = randi(); 195 // returns <number> 196 197 r = randi(); 198 // returns <number> 199 200 // ... 201 202 // Get a copy of the current state: 203 var state = randi.state; 204 205 r = randi(); 206 // returns <number> 207 208 r = randi(); 209 // returns <number> 210 211 // Reset the state: 212 randi.state = state; 213 214 // Replay the last two pseudorandom numbers: 215 r = randi(); 216 // returns <number> 217 218 r = randi(); 219 // returns <number> 220 221 // ... 222 ``` 223 224 #### randi.stateLength 225 226 Length of generator state. 227 228 ```javascript 229 var len = randi.stateLength; 230 // returns <number> 231 ``` 232 233 #### randi.byteLength 234 235 Size (in bytes) of generator state. 236 237 ```javascript 238 var sz = randi.byteLength; 239 // returns <number> 240 ``` 241 242 #### randi.toJSON() 243 244 Serializes the pseudorandom number generator as a JSON object. 245 246 ```javascript 247 var o = randi.toJSON(); 248 // returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } 249 ``` 250 251 </section> 252 253 <!-- /.usage --> 254 255 <section class="notes"> 256 257 ## Notes 258 259 - **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. 260 - If PRNG state is "shared" (meaning a state array was provided during PRNG creation and **not** copied) and one sets the generator state to a state array having a different length, the PRNG does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize PRNG output according to the new shared state array, the state array for **each** relevant PRNG must be **explicitly** set. 261 - If PRNG state is "shared" and one sets the generator state to a state array of the same length, the PRNG state is updated (along with the state of all other PRNGs sharing the PRNG's state array). 262 263 </section> 264 265 <!-- /.notes --> 266 267 <section class="examples"> 268 269 ## Examples 270 271 <!-- eslint no-undef: "error" --> 272 273 ```javascript 274 var randi = require( '@stdlib/random/base/randi' ); 275 276 var seed; 277 var rand; 278 var i; 279 280 // Generate pseudorandom numbers... 281 for ( i = 0; i < 100; i++ ) { 282 console.log( randi() ); 283 } 284 285 // Create a new pseudorandom number generator... 286 seed = 1234; 287 rand = randi.factory({ 288 'seed': seed 289 }); 290 for ( i = 0; i < 100; i++ ) { 291 console.log( rand() ); 292 } 293 294 // Create another pseudorandom number generator using a previous seed... 295 rand = randi.factory({ 296 'seed': randi.seed 297 }); 298 for ( i = 0; i < 100; i++ ) { 299 console.log( rand() ); 300 } 301 ``` 302 303 </section> 304 305 <!-- /.examples --> 306 307 <section class="links"> 308 309 [@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/mt19937 310 311 [@stdlib/random/base/minstd]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd 312 313 [@stdlib/random/base/minstd-shuffle]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd-shuffle 314 315 </section> 316 317 <!-- /.links -->