time-to-botec

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

README.md (14619B)


      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 # Laplace Random Numbers
     22 
     23 > Create a [readable stream][readable-stream] for generating pseudorandom numbers drawn from a [Laplace (double exponential)][laplace] distribution.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var randomStream = require( '@stdlib/random/streams/laplace' );
     31 ```
     32 
     33 <a name="random-stream"></a>
     34 
     35 #### randomStream( mu, b\[, options] )
     36 
     37 Returns a [readable stream][readable-stream] for generating pseudorandom numbers drawn from a [Laplace (double exponential)][laplace] distribution with parameters `mu` (mean) and `b` (scale).
     38 
     39 ```javascript
     40 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     41 
     42 var iStream;
     43 var stream;
     44 
     45 function log( chunk, idx ) {
     46     console.log( chunk.toString() );
     47     if ( idx === 10 ) {
     48         stream.destroy();
     49     }
     50 }
     51 
     52 stream = randomStream( -2.0, 2.0 );
     53 iStream = inspectStream( log );
     54 
     55 stream.pipe( iStream );
     56 ```
     57 
     58 The function accepts the following `options`:
     59 
     60 -   **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`.
     61 -   **encoding**: specifies how `Buffer` objects should be decoded to `strings`. Default: `null`.
     62 -   **highWaterMark**: specifies the maximum number of bytes to store in an internal buffer before ceasing to generate additional pseudorandom numbers.
     63 -   **sep**: separator used to join streamed data. This option is only applicable when a stream is **not** in [objectMode][object-mode]. Default: `'\n'`.
     64 -   **iter**: number of iterations.
     65 -   **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 stream, one must seed the provided `prng` (assuming the provided `prng` is seedable).
     66 -   **seed**: pseudorandom number generator seed.
     67 -   **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
     68 -   **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 and/or streams. Setting this option to `true` ensures that a stream generator has exclusive control over its internal state. Default: `true`.
     69 -   **siter**: number of iterations after which to emit the pseudorandom number generator state. This option is useful when wanting to deterministically capture a stream's underlying PRNG state. Default: `1e308`.
     70 
     71 To set [stream][stream] `options`,
     72 
     73 ```javascript
     74 var opts = {
     75     'objectMode': true,
     76     'encoding': 'utf8',
     77     'highWaterMark': 64
     78 };
     79 
     80 var stream = randomStream( 0.0, 2.0, opts );
     81 ```
     82 
     83 By default, the function returns a [stream][stream] which can generate an infinite number of values (i.e., the [stream][stream] will **never** end). To limit the number of generated pseudorandom numbers, set the `iter` option.
     84 
     85 ```javascript
     86 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     87 
     88 function log( chunk ) {
     89     console.log( chunk.toString() );
     90 }
     91 
     92 var opts = {
     93     'iter': 10
     94 };
     95 
     96 var stream = randomStream( 0.0, 1.0, opts );
     97 var iStream = inspectStream( log );
     98 
     99 stream.pipe( iStream );
    100 ```
    101 
    102 By default, when not operating in [objectMode][object-mode], a returned [stream][stream] delineates generated pseudorandom numbers using a newline character. To specify an alternative separator, set the `sep` option.
    103 
    104 ```javascript
    105 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    106 
    107 function log( chunk ) {
    108     console.log( chunk.toString() );
    109 }
    110 
    111 var opts = {
    112     'iter': 10,
    113     'sep': ','
    114 };
    115 
    116 var stream = randomStream( 2.0, 1.0, opts );
    117 var iStream = inspectStream( log );
    118 
    119 stream.pipe( iStream );
    120 ```
    121 
    122 To seed the underlying pseudorandom number generator, set the `seed` option.
    123 
    124 ```javascript
    125 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    126 
    127 function log( v ) {
    128     console.log( v );
    129 }
    130 
    131 var opts = {
    132     'objectMode': true,
    133     'iter': 10,
    134     'seed': 1234
    135 };
    136 
    137 var stream = randomStream( 2.0, 1.0, opts );
    138 
    139 opts = {
    140     'objectMode': true
    141 };
    142 var iStream = inspectStream( opts, log );
    143 
    144 stream.pipe( iStream );
    145 ```
    146 
    147 To return a [readable stream][readable-stream] with an underlying pseudorandom number generator having a specific initial state, set the `state` option.
    148 
    149 ```javascript
    150 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    151 
    152 function log( v ) {
    153     console.log( v );
    154 }
    155 
    156 var opts1 = {
    157     'objectMode': true,
    158     'iter': 10
    159 };
    160 
    161 var stream = randomStream( 2.0, 2.0, opts1 );
    162 
    163 var opts2 = {
    164     'objectMode': true
    165 };
    166 var iStream = inspectStream( opts2, log );
    167 
    168 // Stream pseudorandom numbers, thus progressing the underlying generator state:
    169 stream.pipe( iStream );
    170 
    171 // Create a new PRNG stream initialized to the last state of the previous stream:
    172 var opts3 = {
    173     'objectMode': true,
    174     'iter': 10,
    175     'state': stream.state
    176 };
    177 
    178 stream = randomStream( 2.0, 2.0, opts3 );
    179 iStream = inspectStream( opts2, log );
    180 
    181 // Stream pseudorandom numbers starting from the last state of the previous stream:
    182 stream.pipe( iStream );
    183 ```
    184 
    185 ##### stream.PRNG
    186 
    187 The underlying pseudorandom number generator.
    188 
    189 ```javascript
    190 var stream = randomStream( 2.0, 2.0 );
    191 
    192 var prng = stream.PRNG;
    193 // returns <Function>
    194 ```
    195 
    196 ##### stream.seed
    197 
    198 The value used to seed the underlying pseudorandom number generator.
    199 
    200 ```javascript
    201 var stream = randomStream( 2.0, 2.0 );
    202 
    203 var seed = stream.seed;
    204 // returns <Uint32Array>
    205 ```
    206 
    207 If provided a PRNG for uniformly distributed numbers, this value is `null`.
    208 
    209 ```javascript
    210 var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
    211 
    212 var stream = randomStream( 2.0, 2.0, {
    213     'prng': minstd
    214 });
    215 
    216 var seed = stream.seed;
    217 // returns null
    218 ```
    219 
    220 ##### stream.seedLength
    221 
    222 Length of underlying pseudorandom number generator seed.
    223 
    224 ```javascript
    225 var stream = randomStream( 2.0, 2.0 );
    226 
    227 var len = stream.seedLength;
    228 // returns <number>
    229 ```
    230 
    231 If provided a PRNG for uniformly distributed numbers, this value is `null`.
    232 
    233 ```javascript
    234 var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
    235 
    236 var stream = randomStream( 2.0, 2.0, {
    237     'prng': minstd
    238 });
    239 
    240 var len = stream.seedLength;
    241 // returns null
    242 ```
    243 
    244 ##### stream.state
    245 
    246 Writable property for getting and setting the underlying pseudorandom number generator state.
    247 
    248 ```javascript
    249 var stream = randomStream( 2.0, 2.0 );
    250 
    251 var state = stream.state;
    252 // returns <Uint32Array>
    253 ```
    254 
    255 If provided a PRNG for uniformly distributed numbers, this value is `null`.
    256 
    257 ```javascript
    258 var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
    259 
    260 var stream = randomStream( 2.0, 2.0, {
    261     'prng': minstd
    262 });
    263 
    264 var state = stream.state;
    265 // returns null
    266 ```
    267 
    268 ##### stream.stateLength
    269 
    270 Length of underlying pseudorandom number generator state.
    271 
    272 ```javascript
    273 var stream = randomStream( 2.0, 2.0 );
    274 
    275 var len = stream.stateLength;
    276 // returns <number>
    277 ```
    278 
    279 If provided a PRNG for uniformly distributed numbers, this value is `null`.
    280 
    281 ```javascript
    282 var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
    283 
    284 var stream = randomStream( 2.0, 2.0, {
    285     'prng': minstd
    286 });
    287 
    288 var len = stream.stateLength;
    289 // returns null
    290 ```
    291 
    292 ##### stream.byteLength
    293 
    294 Size (in bytes) of underlying pseudorandom number generator state.
    295 
    296 ```javascript
    297 var stream = randomStream( 2.0, 2.0 );
    298 
    299 var sz = stream.byteLength;
    300 // returns <number>
    301 ```
    302 
    303 If provided a PRNG for uniformly distributed numbers, this value is `null`.
    304 
    305 ```javascript
    306 var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
    307 
    308 var stream = randomStream( 2.0, 2.0, {
    309     'prng': minstd
    310 });
    311 
    312 var sz = stream.byteLength;
    313 // returns null
    314 ```
    315 
    316 * * *
    317 
    318 #### randomStream.factory( \[mu, b, ]\[options] )
    319 
    320 Returns a `function` for creating [readable streams][readable-stream] which generate pseudorandom numbers drawn from a [Laplace (double exponential)][laplace] distribution.
    321 
    322 ```javascript
    323 var opts = {
    324     'objectMode': true,
    325     'encoding': 'utf8',
    326     'highWaterMark': 64
    327 };
    328 
    329 var createStream = randomStream.factory( opts );
    330 ```
    331 
    332 If provided distribution parameters, the returned function returns [readable streams][readable-stream] which generate pseudorandom numbers drawn from the specified distribution.
    333 
    334 ```javascript
    335 var createStream = randomStream.factory( 2.0, 2.0 );
    336 
    337 var stream1 = createStream();
    338 var stream2 = createStream();
    339 // ...
    340 ```
    341 
    342 If not provided distribution parameters, the returned function requires that distribution parameters be provided at each invocation.
    343 
    344 ```javascript
    345 var createStream = randomStream.factory();
    346 
    347 var stream1 = createStream( 2.0, 2.0 );
    348 var stream2 = createStream( 2.0, 2.0 );
    349 // ...
    350 ```
    351 
    352 The method accepts the same `options` as [`randomStream()`](#random-stream).
    353 
    354 * * *
    355 
    356 #### randomStream.objectMode( mu, b\[, options] )
    357 
    358 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode].
    359 
    360 ```javascript
    361 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    362 
    363 function log( v ) {
    364     console.log( v );
    365 }
    366 
    367 var opts = {
    368     'iter': 10
    369 };
    370 var stream = randomStream.objectMode( 2.0, 2.0, opts );
    371 
    372 opts = {
    373     'objectMode': true
    374 };
    375 var iStream = inspectStream( opts, log );
    376 
    377 stream.pipe( iStream );
    378 ```
    379 
    380 This method accepts the same `options` as [`randomStream()`](#random-stream); however, the method will **always** override the [`objectMode`][object-mode] option in `options`.
    381 
    382 * * *
    383 
    384 ### Events
    385 
    386 In addition to the standard [readable stream][readable-stream] events, the following events are supported...
    387 
    388 #### 'state'
    389 
    390 Emitted after internally generating `siter` pseudorandom numbers.
    391 
    392 ```javascript
    393 var opts = {
    394     'siter': 10 // emit the PRNG state every 10 pseudorandom numbers
    395 };
    396 
    397 var stream = randomStream( 2.0, 2.0, opts );
    398 
    399 stream.on( 'state', onState );
    400 
    401 function onState( state ) {
    402     // Do something with the emitted state, such as save to file...
    403 }
    404 ```
    405 
    406 </section>
    407 
    408 <!-- /.usage -->
    409 
    410 * * *
    411 
    412 <section class="notes">
    413 
    414 ## Notes
    415 
    416 -   If PRNG state is "shared" (meaning a state array was provided during stream creation and **not** copied) and one sets the generator state to a state array having a different length, the underlying 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.
    417 -   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).
    418 -   In order to capture the PRNG state after a specific number of generated pseudorandom numbers, regardless of internal stream buffering, use the `siter` option in conjunction with a `state` event listener. Attempting to capture the underlying PRNG state after **reading** generated numbers is **not** likely to give expected results, as internal stream buffering will mean more values have been generated than have been read. Thus, the state returned by the `state` property will likely reflect a future PRNG state from the perspective of downstream consumers.
    419 
    420 </section>
    421 
    422 <!-- /.notes -->
    423 
    424 * * *
    425 
    426 <section class="examples">
    427 
    428 ## Examples
    429 
    430 <!-- eslint no-undef: "error" -->
    431 
    432 ```javascript
    433 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    434 var randomStream = require( '@stdlib/random/streams/laplace' );
    435 
    436 function log( v ) {
    437     console.log( v.toString() );
    438 }
    439 
    440 var opts = {
    441     'objectMode': true,
    442     'iter': 10
    443 };
    444 
    445 var stream = randomStream( 2.0, 2.0, opts );
    446 
    447 opts = {
    448     'objectMode': true
    449 };
    450 var iStream = inspectStream( opts, log );
    451 
    452 stream.pipe( iStream );
    453 ```
    454 
    455 </section>
    456 
    457 <!-- /.examples -->
    458 
    459 <!-- Section for describing a command-line interface. -->
    460 
    461 * * *
    462 
    463 <section class="cli">
    464 
    465 ## CLI
    466 
    467 <!-- CLI usage documentation. -->
    468 
    469 <section class="usage">
    470 
    471 ### Usage
    472 
    473 ```text
    474 Usage: random-laplace [options] <mu> <b>
    475 
    476 Options:
    477 
    478   -h,  --help               Print this message.
    479   -V,  --version            Print the package version.
    480        --sep sep            Separator used to join streamed data. Default: '\n'.
    481   -n,  --iter iterations    Number of pseudorandom numbers.
    482        --seed seed          Pseudorandom number generator seed.
    483        --state filepath     Path to a file containing the pseudorandom number
    484                             generator state.
    485        --snapshot filepath  Output file path for saving the pseudorandom number
    486                             generator state upon exit.
    487 ```
    488 
    489 </section>
    490 
    491 <!-- /.usage -->
    492 
    493 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    494 
    495 <section class="notes">
    496 
    497 ### Notes
    498 
    499 -   In accordance with POSIX convention, a trailing newline is **always** appended to generated output prior to exit.
    500 -   Specifying a "snapshot" file path is useful when wanting to resume pseudorandom number generation due to, e.g., a downstream failure in an analysis pipeline. Before exiting, the process will store the pseudorandom number generator state in a file specified according to a provided file path. Upon loading a snapshot (state), the process will generate pseudorandom numbers starting from the loaded state, thus avoiding having to seed and replay an entire analysis.
    501 
    502 </section>
    503 
    504 <!-- /.notes -->
    505 
    506 <!-- CLI usage examples. -->
    507 
    508 <section class="examples">
    509 
    510 ### Examples
    511 
    512 ```bash
    513 $ random-laplace 0.0 1.0 -n 10 --seed 1234
    514 ```
    515 
    516 </section>
    517 
    518 <!-- /.examples -->
    519 
    520 </section>
    521 
    522 <!-- /.cli -->
    523 
    524 <section class="links">
    525 
    526 [stream]: https://nodejs.org/api/stream.html
    527 
    528 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode
    529 
    530 [readable-stream]: https://nodejs.org/api/stream.html
    531 
    532 [laplace]: https://en.wikipedia.org/wiki/Laplace_distribution
    533 
    534 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32
    535 
    536 </section>
    537 
    538 <!-- /.links -->