time-to-botec

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

README.md (15096B)


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