time-to-botec

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

README.md (13493B)


      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 # MT19937
     22 
     23 > Create a [readable stream][readable-stream] for a 32-bit [Mersenne Twister][@stdlib/random/base/mt19937] pseudorandom number generator.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var randomStream = require( '@stdlib/random/streams/mt19937' );
     31 ```
     32 
     33 <a name="random-stream"></a>
     34 
     35 #### randomStream( \[options] )
     36 
     37 Returns a [readable stream][readable-stream] for a 32-bit [Mersenne Twister][@stdlib/random/base/mt19937] pseudorandom number generator.
     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 -   **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 -   **normalized**: `boolean` indicating whether to return pseudorandom numbers on the interval `[0,1)`.
     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( 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( opts );
     97 var iStream = inspectStream( log );
     98 
     99 stream.pipe( iStream );
    100 ```
    101 
    102 To return pseudorandom numbers on the interval `[0,1)`, set the `normalized` 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     'normalized': true
    114 };
    115 
    116 var stream = randomStream( opts );
    117 var iStream = inspectStream( log );
    118 
    119 stream.pipe( iStream );
    120 ```
    121 
    122 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.
    123 
    124 ```javascript
    125 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    126 
    127 function log( chunk ) {
    128     console.log( chunk.toString() );
    129 }
    130 
    131 var opts = {
    132     'iter': 10,
    133     'sep': ','
    134 };
    135 
    136 var stream = randomStream( opts );
    137 var iStream = inspectStream( log );
    138 
    139 stream.pipe( iStream );
    140 ```
    141 
    142 To seed the underlying pseudorandom number generator, set the `seed` option.
    143 
    144 ```javascript
    145 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    146 
    147 function log( v ) {
    148     console.log( v );
    149 }
    150 
    151 var opts = {
    152     'objectMode': true,
    153     'iter': 10,
    154     'seed': 1234
    155 };
    156 
    157 var stream = randomStream( opts );
    158 
    159 opts = {
    160     'objectMode': true
    161 };
    162 var iStream = inspectStream( opts, log );
    163 
    164 stream.pipe( iStream );
    165 ```
    166 
    167 To return a [readable stream][readable-stream] with an underlying pseudorandom number generator having a specific initial state, set the `state` option.
    168 
    169 ```javascript
    170 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    171 
    172 function log( v ) {
    173     console.log( v );
    174 }
    175 
    176 var opts1 = {
    177     'objectMode': true,
    178     'iter': 10
    179 };
    180 
    181 var stream = randomStream( opts1 );
    182 
    183 var opts2 = {
    184     'objectMode': true
    185 };
    186 var iStream = inspectStream( opts2, log );
    187 
    188 // Stream pseudorandom numbers, thus progressing the underlying generator state:
    189 stream.pipe( iStream );
    190 
    191 // Create a new PRNG stream initialized to the last state of the previous stream:
    192 var opts3 = {
    193     'objectMode': true,
    194     'iter': 10,
    195     'state': stream.state
    196 };
    197 
    198 stream = randomStream( opts3 );
    199 iStream = inspectStream( opts2, log );
    200 
    201 // Stream pseudorandom numbers starting from the last state of the previous stream:
    202 stream.pipe( iStream );
    203 ```
    204 
    205 ##### stream.seed
    206 
    207 The value used to seed the underlying pseudorandom number generator.
    208 
    209 ```javascript
    210 var stream = randomStream();
    211 
    212 var seed = stream.seed;
    213 // returns <Uint32Array>
    214 ```
    215 
    216 ##### stream.seedLength
    217 
    218 Length of underlying pseudorandom number generator seed.
    219 
    220 ```javascript
    221 var stream = randomStream();
    222 
    223 var len = stream.seedLength;
    224 // returns <number>
    225 ```
    226 
    227 ##### stream.state
    228 
    229 Writable property for getting and setting the underlying pseudorandom number generator state.
    230 
    231 ```javascript
    232 var stream = randomStream();
    233 
    234 var state = stream.state;
    235 // returns <Uint32Array>
    236 ```
    237 
    238 ##### stream.stateLength
    239 
    240 Length of underlying pseudorandom number generator state.
    241 
    242 ```javascript
    243 var stream = randomStream();
    244 
    245 var len = stream.stateLength;
    246 // returns <number>
    247 ```
    248 
    249 ##### stream.byteLength
    250 
    251 Size (in bytes) of underlying pseudorandom number generator state.
    252 
    253 ```javascript
    254 var stream = randomStream();
    255 
    256 var sz = stream.byteLength;
    257 // returns <number>
    258 ```
    259 
    260 * * *
    261 
    262 #### randomStream.factory( \[options] )
    263 
    264 Returns a `function` for creating [readable streams][readable-stream] which generate pseudorandom numbers via a 32-bit [Mersenne Twister][@stdlib/random/base/mt19937] pseudorandom number generator.
    265 
    266 ```javascript
    267 var opts = {
    268     'objectMode': true,
    269     'encoding': 'utf8',
    270     'highWaterMark': 64
    271 };
    272 
    273 var createStream = randomStream.factory( opts );
    274 ```
    275 
    276 The method accepts the same `options` as [`randomStream()`](#random-stream).
    277 
    278 * * *
    279 
    280 #### randomStream.objectMode( \[options] )
    281 
    282 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode].
    283 
    284 ```javascript
    285 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    286 
    287 function log( v ) {
    288     console.log( v );
    289 }
    290 
    291 var opts = {
    292     'iter': 10
    293 };
    294 var stream = randomStream.objectMode( opts );
    295 
    296 opts = {
    297     'objectMode': true
    298 };
    299 var iStream = inspectStream( opts, log );
    300 
    301 stream.pipe( iStream );
    302 ```
    303 
    304 This method accepts the same `options` as [`randomStream()`](#random-stream); however, the method will **always** override the [`objectMode`][object-mode] option in `options`.
    305 
    306 * * *
    307 
    308 ### Events
    309 
    310 In addition to the standard [readable stream][readable-stream] events, the following events are supported...
    311 
    312 #### 'state'
    313 
    314 Emitted after internally generating `siter` pseudorandom numbers.
    315 
    316 ```javascript
    317 var opts = {
    318     'siter': 10 // emit the PRNG state every 10 pseudorandom numbers
    319 };
    320 
    321 var stream = randomStream( opts );
    322 
    323 stream.on( 'state', onState );
    324 
    325 function onState( state ) {
    326     // Do something with the emitted state, such as save to file...
    327 }
    328 ```
    329 
    330 </section>
    331 
    332 <!-- /.usage -->
    333 
    334 * * *
    335 
    336 <section class="notes">
    337 
    338 ## Notes
    339 
    340 -   [Mersenne Twister][@stdlib/random/base/mt19937] is **not** a cryptographically secure PRNG, as the PRNG is based on a linear recursion. Any pseudorandom number sequence generated by a linear recursion is **insecure**, due to the fact that one can predict future generated outputs by observing a sufficiently long subsequence of generated values.
    341 -   The underlying PRNG has a period of `2^19937 - 1`.
    342 -   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.
    343 -   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).
    344 -   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.
    345 
    346 </section>
    347 
    348 <!-- /.notes -->
    349 
    350 * * *
    351 
    352 <section class="examples">
    353 
    354 ## Examples
    355 
    356 <!-- eslint no-undef: "error" -->
    357 
    358 ```javascript
    359 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    360 var randomStream = require( '@stdlib/random/streams/mt19937' );
    361 
    362 function log( v ) {
    363     console.log( v.toString() );
    364 }
    365 
    366 var opts = {
    367     'objectMode': true,
    368     'iter': 10
    369 };
    370 
    371 var stream = randomStream( opts );
    372 
    373 opts = {
    374     'objectMode': true
    375 };
    376 var iStream = inspectStream( opts, log );
    377 
    378 stream.pipe( iStream );
    379 ```
    380 
    381 </section>
    382 
    383 <!-- /.examples -->
    384 
    385 <!-- Section for describing a command-line interface. -->
    386 
    387 * * *
    388 
    389 <section class="cli">
    390 
    391 ## CLI
    392 
    393 <!-- CLI usage documentation. -->
    394 
    395 <section class="usage">
    396 
    397 ### Usage
    398 
    399 ```text
    400 Usage: random-mt19937 [options]
    401 
    402 Options:
    403 
    404   -h,  --help               Print this message.
    405   -V,  --version            Print the package version.
    406        --sep sep            Separator used to join streamed data. Default: '\n'.
    407   -n,  --iter iterations    Number of pseudorandom numbers.
    408        --normalized         Generate pseudorandom numbers on the interval [0,1).
    409        --seed seed          Pseudorandom number generator seed.
    410        --state filepath     Path to a file containing the pseudorandom number
    411                             generator state.
    412        --snapshot filepath  Output file path for saving the pseudorandom number
    413                             generator state upon exit.
    414 ```
    415 
    416 </section>
    417 
    418 <!-- /.usage -->
    419 
    420 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    421 
    422 <section class="notes">
    423 
    424 ### Notes
    425 
    426 -   In accordance with POSIX convention, a trailing newline is **always** appended to generated output prior to exit.
    427 -   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.
    428 
    429 </section>
    430 
    431 <!-- /.notes -->
    432 
    433 <!-- CLI usage examples. -->
    434 
    435 <section class="examples">
    436 
    437 ### Examples
    438 
    439 ```bash
    440 $ random-mt19937 -n 10 --seed 1234
    441 ```
    442 
    443 </section>
    444 
    445 <!-- /.examples -->
    446 
    447 </section>
    448 
    449 <!-- /.cli -->
    450 
    451 * * *
    452 
    453 <section class="references">
    454 
    455 ## References
    456 
    457 -   Matsumoto, Makoto, and Takuji Nishimura. 1998. "Mersenne Twister: A 623-dimensionally Equidistributed Uniform Pseudo-random Number Generator." _ACM Transactions on Modeling and Computer Simulation_ 8 (1). New York, NY, USA: ACM: 3–30. doi:[10.1145/272991.272995][@matsumoto:1998a].
    458 -   Harase, Shin. 2017. "Conversion of Mersenne Twister to double-precision floating-point numbers." _ArXiv_ abs/1708.06018 (September). <https://arxiv.org/abs/1708.06018>.
    459 
    460 </section>
    461 
    462 <!-- /.references -->
    463 
    464 <section class="links">
    465 
    466 [stream]: https://nodejs.org/api/stream.html
    467 
    468 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode
    469 
    470 [readable-stream]: https://nodejs.org/api/stream.html
    471 
    472 [@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/mt19937
    473 
    474 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32
    475 
    476 [@matsumoto:1998a]: https://doi.org/10.1145/272991.272995
    477 
    478 </section>
    479 
    480 <!-- /.links -->