time-to-botec

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

README.md (8923B)


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