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