time-to-botec

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

README.md (13265B)


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