time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (8993B)


      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 # Exponential Random Numbers
     22 
     23 > [Exponentially][exponential] distributed pseudorandom numbers.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var exponential = require( '@stdlib/random/base/exponential' );
     31 ```
     32 
     33 #### exponential( lambda )
     34 
     35 Returns a pseudorandom number drawn from an [exponential][exponential] distribution with rate parameter `lambda`.
     36 
     37 ```javascript
     38 var r = exponential( 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 = exponential( -2.0 );
     46 // returns NaN
     47 
     48 r = exponential( NaN );
     49 // returns NaN
     50 ```
     51 
     52 #### exponential.factory( \[lambda, ]\[options] )
     53 
     54 Returns a pseudorandom number generator (PRNG) for generating pseudorandom numbers drawn from an [exponential][exponential] distribution.
     55 
     56 ```javascript
     57 var rand = exponential.factory();
     58 
     59 var r = rand( 5.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 = exponential.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 = exponential.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 = exponential.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 = exponential.factory({
    111     'seed': 12345
    112 });
    113 
    114 var r1 = rand1( 3.0 );
    115 // returns <number>
    116 
    117 var rand2 = exponential.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 = exponential( 3.0 );
    139 }
    140 
    141 // Create a new PRNG initialized to the current state of `exponential`:
    142 rand = exponential.factory({
    143     'state': exponential.state
    144 });
    145 
    146 // Test that the generated pseudorandom numbers are the same:
    147 bool = ( rand( 3.0 ) === exponential( 3.0 ) );
    148 // returns true
    149 ```
    150 
    151 #### exponential.NAME
    152 
    153 The generator name.
    154 
    155 ```javascript
    156 var str = exponential.NAME;
    157 // returns 'exponential'
    158 ```
    159 
    160 #### exponential.PRNG
    161 
    162 The underlying pseudorandom number generator.
    163 
    164 ```javascript
    165 var prng = exponential.PRNG;
    166 // returns <Function>
    167 ```
    168 
    169 #### exponential.seed
    170 
    171 The value used to seed `exponential()`.
    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 = exponential( 2.0 );
    181 }
    182 
    183 // Generate the same pseudorandom values...
    184 rand = exponential.factory( 2.0, {
    185     'seed': exponential.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 = exponential.factory({
    198     'prng': Math.random
    199 });
    200 
    201 var seed = rand.seed;
    202 // returns null
    203 ```
    204 
    205 #### exponential.seedLength
    206 
    207 Length of generator seed.
    208 
    209 ```javascript
    210 var len = exponential.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 = exponential.factory({
    220     'prng': Math.random
    221 });
    222 
    223 var len = rand.seedLength;
    224 // returns null
    225 ```
    226 
    227 #### exponential.state
    228 
    229 Writable property for getting and setting the generator state.
    230 
    231 ```javascript
    232 var r = exponential( 7.9 );
    233 // returns <number>
    234 
    235 r = exponential( 7.9 );
    236 // returns <number>
    237 
    238 // ...
    239 
    240 // Get a copy of the current state:
    241 var state = exponential.state;
    242 // returns <Uint32Array>
    243 
    244 r = exponential( 7.9 );
    245 // returns <number>
    246 
    247 r = exponential( 7.9 );
    248 // returns <number>
    249 
    250 // Reset the state:
    251 exponential.state = state;
    252 
    253 // Replay the last two pseudorandom numbers:
    254 r = exponential( 7.9 );
    255 // returns <number>
    256 
    257 r = exponential( 7.9 );
    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 = exponential.factory({
    269     'prng': Math.random
    270 });
    271 
    272 var state = rand.state;
    273 // returns null
    274 ```
    275 
    276 #### exponential.stateLength
    277 
    278 Length of generator state.
    279 
    280 ```javascript
    281 var len = exponential.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 = exponential.factory({
    291     'prng': Math.random
    292 });
    293 
    294 var len = rand.stateLength;
    295 // returns null
    296 ```
    297 
    298 #### exponential.byteLength
    299 
    300 Size (in bytes) of generator state.
    301 
    302 ```javascript
    303 var sz = exponential.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 = exponential.factory({
    313     'prng': Math.random
    314 });
    315 
    316 var sz = rand.byteLength;
    317 // returns null
    318 ```
    319 
    320 #### exponential.toJSON()
    321 
    322 Serializes the pseudorandom number generator as a JSON object.
    323 
    324 ```javascript
    325 var o = exponential.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 = exponential.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 exponential = require( '@stdlib/random/base/exponential' );
    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( exponential( 0.5 ) );
    373 }
    374 
    375 // Create a new pseudorandom number generator...
    376 seed = 1234;
    377 rand = exponential.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 = exponential.factory( 0.5, {
    386     'seed': exponential.seed
    387 });
    388 for ( i = 0; i < 100; i++ ) {
    389     console.log( rand() );
    390 }
    391 ```
    392 
    393 </section>
    394 
    395 <!-- /.examples -->
    396 
    397 <section class="links">
    398 
    399 [exponential]: https://en.wikipedia.org/wiki/Exponential_distribution
    400 
    401 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32
    402 
    403 </section>
    404 
    405 <!-- /.links -->