README.md (9073B)
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 # Erlang Random Numbers 22 23 > [Erlang][erlang] distributed pseudorandom numbers. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var erlang = require( '@stdlib/random/base/erlang' ); 31 ``` 32 33 #### erlang( k, lambda ) 34 35 Returns a pseudorandom number drawn from a [Erlang][erlang] distribution with parameters `k` (shape parameter) and `lambda` (rate parameter). 36 37 ```javascript 38 var r = erlang( 2, 5.0 ); 39 // returns <number> 40 ``` 41 42 If `k` is not a positive integer, the function returns `NaN`. 43 44 ```javascript 45 var r = erlang( 2.2, 1.0 ); 46 // returns NaN 47 ``` 48 49 If `lambda <= 0`, the function returns `NaN`. 50 51 ```javascript 52 var r = erlang( 2, -2.0 ); 53 // returns NaN 54 ``` 55 56 If either `k` or `lambda` is `NaN`, the function returns `NaN`. 57 58 ```javascript 59 var r = erlang( NaN, 5.0 ); 60 // returns NaN 61 62 r = erlang( 2, NaN ); 63 // returns NaN 64 ``` 65 66 #### erlang.factory( \[k, lambda, ]\[options] ) 67 68 Returns a pseudorandom number generator (PRNG) for generating pseudorandom numbers drawn from a [Erlang][erlang] distribution. 69 70 ```javascript 71 var rand = erlang.factory(); 72 73 var r = rand( 2, 1.5 ); 74 // returns <number> 75 ``` 76 77 If provided `k` and `lambda`, the returned generator returns random variates from the specified distribution. 78 79 ```javascript 80 // Draw from Erlang( 2, 1.5 ) distribution: 81 var rand = erlang.factory( 2, 1.5 ); 82 83 var r = rand(); 84 // returns <number> 85 86 r = rand(); 87 // returns <number> 88 ``` 89 90 If not provided `k` and `lambda`, the returned generator requires that both parameters be provided at each invocation. 91 92 ```javascript 93 var rand = erlang.factory(); 94 95 var r = rand( 2, 1.0 ); 96 // returns <number> 97 98 r = rand( 4, 3.14 ); 99 // returns <number> 100 ``` 101 102 The function accepts the following `options`: 103 104 - **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 pseudorandom number generator, one must seed the provided `prng` (assuming the provided `prng` is seedable). 105 - **seed**: pseudorandom number generator seed. 106 - **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. 107 - **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`. 108 109 To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option. 110 111 ```javascript 112 var minstd = require( '@stdlib/random/base/minstd' ); 113 114 var rand = erlang.factory({ 115 'prng': minstd.normalized 116 }); 117 118 var r = rand( 2, 3.0 ); 119 // returns <number> 120 ``` 121 122 To seed a pseudorandom number generator, set the `seed` option. 123 124 ```javascript 125 var rand1 = erlang.factory({ 126 'seed': 12345 127 }); 128 129 var r1 = rand1( 2, 3.0 ); 130 // returns <number> 131 132 var rand2 = erlang.factory( 2, 3.0, { 133 'seed': 12345 134 }); 135 136 var r2 = rand2(); 137 // returns <number> 138 139 var bool = ( r1 === r2 ); 140 // returns true 141 ``` 142 143 To return a generator having a specific initial state, set the generator `state` option. 144 145 ```javascript 146 var rand; 147 var bool; 148 var r; 149 var i; 150 151 // Generate pseudorandom numbers, thus progressing the generator state: 152 for ( i = 0; i < 1000; i++ ) { 153 r = erlang( 2, 3.0 ); 154 } 155 156 // Create a new PRNG initialized to the current state of `erlang`: 157 rand = erlang.factory({ 158 'state': erlang.state 159 }); 160 161 // Test that the generated pseudorandom numbers are the same: 162 bool = ( rand( 2, 3.0 ) === erlang( 2, 3.0 ) ); 163 // returns true 164 ``` 165 166 #### erlang.NAME 167 168 The generator name. 169 170 ```javascript 171 var str = erlang.NAME; 172 // returns 'erlang' 173 ``` 174 175 #### erlang.PRNG 176 177 The underlying pseudorandom number generator. 178 179 ```javascript 180 var prng = erlang.PRNG; 181 // returns <Function> 182 ``` 183 184 #### erlang.seed 185 186 The value used to seed `erlang()`. 187 188 ```javascript 189 var rand; 190 var r; 191 var i; 192 193 // Generate pseudorandom values... 194 for ( i = 0; i < 100; i++ ) { 195 r = erlang( 2, 2.0 ); 196 } 197 198 // Generate the same pseudorandom values... 199 rand = erlang.factory( 2, 2.0, { 200 'seed': erlang.seed 201 }); 202 for ( i = 0; i < 100; i++ ) { 203 r = rand(); 204 } 205 ``` 206 207 If provided a PRNG for uniformly distributed numbers, this value is `null`. 208 209 <!-- eslint-disable stdlib/no-builtin-math --> 210 211 ```javascript 212 var rand = erlang.factory({ 213 'prng': Math.random 214 }); 215 216 var seed = rand.seed; 217 // returns null 218 ``` 219 220 #### erlang.seedLength 221 222 Length of generator seed. 223 224 ```javascript 225 var len = erlang.seedLength; 226 // returns <number> 227 ``` 228 229 If provided a PRNG for uniformly distributed numbers, this value is `null`. 230 231 <!-- eslint-disable stdlib/no-builtin-math --> 232 233 ```javascript 234 var rand = erlang.factory({ 235 'prng': Math.random 236 }); 237 238 var len = rand.seedLength; 239 // returns null 240 ``` 241 242 #### erlang.state 243 244 Writable property for getting and setting the generator state. 245 246 ```javascript 247 var r = erlang( 2, 4.0 ); 248 // returns <number> 249 250 r = erlang( 2, 4.0 ); 251 // returns <number> 252 253 // ... 254 255 // Get a copy of the current state: 256 var state = erlang.state; 257 // returns <Uint32Array> 258 259 r = erlang( 2, 4.0 ); 260 // returns <number> 261 262 r = erlang( 2, 4.0 ); 263 // returns <number> 264 265 // Reset the state: 266 erlang.state = state; 267 268 // Replay the last two pseudorandom numbers: 269 r = erlang( 2, 4.0 ); 270 // returns <number> 271 272 r = erlang( 2, 4.0 ); 273 // returns <number> 274 275 // ... 276 ``` 277 278 If provided a PRNG for uniformly distributed numbers, this value is `null`. 279 280 <!-- eslint-disable stdlib/no-builtin-math --> 281 282 ```javascript 283 var rand = erlang.factory({ 284 'prng': Math.random 285 }); 286 287 var state = rand.state; 288 // returns null 289 ``` 290 291 #### erlang.stateLength 292 293 Length of generator state. 294 295 ```javascript 296 var len = erlang.stateLength; 297 // returns <number> 298 ``` 299 300 If provided a PRNG for uniformly distributed numbers, this value is `null`. 301 302 <!-- eslint-disable stdlib/no-builtin-math --> 303 304 ```javascript 305 var rand = erlang.factory({ 306 'prng': Math.random 307 }); 308 309 var len = rand.stateLength; 310 // returns null 311 ``` 312 313 #### erlang.byteLength 314 315 Size (in bytes) of generator state. 316 317 ```javascript 318 var sz = erlang.byteLength; 319 // returns <number> 320 ``` 321 322 If provided a PRNG for uniformly distributed numbers, this value is `null`. 323 324 <!-- eslint-disable stdlib/no-builtin-math --> 325 326 ```javascript 327 var rand = erlang.factory({ 328 'prng': Math.random 329 }); 330 331 var sz = rand.byteLength; 332 // returns null 333 ``` 334 335 #### erlang.toJSON() 336 337 Serializes the pseudorandom number generator as a JSON object. 338 339 ```javascript 340 var o = erlang.toJSON(); 341 // returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } 342 ``` 343 344 If provided a PRNG for uniformly distributed numbers, this method returns `null`. 345 346 <!-- eslint-disable stdlib/no-builtin-math --> 347 348 ```javascript 349 var rand = erlang.factory({ 350 'prng': Math.random 351 }); 352 353 var o = rand.toJSON(); 354 // returns null 355 ``` 356 357 </section> 358 359 <!-- /.usage --> 360 361 <section class="notes"> 362 363 ## Notes 364 365 - 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. 366 - 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). 367 368 </section> 369 370 <!-- /.notes --> 371 372 <section class="examples"> 373 374 ## Examples 375 376 <!-- eslint no-undef: "error" --> 377 378 ```javascript 379 var erlang = require( '@stdlib/random/base/erlang' ); 380 381 var seed; 382 var rand; 383 var i; 384 385 // Generate pseudorandom numbers... 386 for ( i = 0; i < 100; i++ ) { 387 console.log( erlang( 2, 2.0 ) ); 388 } 389 390 // Create a new pseudorandom number generator... 391 seed = 1234; 392 rand = erlang.factory( 6, 2.0, { 393 'seed': seed 394 }); 395 for ( i = 0; i < 100; i++ ) { 396 console.log( rand() ); 397 } 398 399 // Create another pseudorandom number generator using a previous seed... 400 rand = erlang.factory( 2, 2.0, { 401 'seed': erlang.seed 402 }); 403 for ( i = 0; i < 100; i++ ) { 404 console.log( rand() ); 405 } 406 ``` 407 408 </section> 409 410 <!-- /.examples --> 411 412 <section class="links"> 413 414 [erlang]: https://en.wikipedia.org/wiki/Erlang_distribution 415 416 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32 417 418 </section> 419 420 <!-- /.links -->