time-to-botec

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

README.md (6462B)


      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 an iterator for a 32-bit [Mersenne Twister][mersenne-twister] pseudorandom number generator.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var iterator = require( '@stdlib/random/iter/mt19937' );
     31 ```
     32 
     33 #### iterator( \[options] )
     34 
     35 Returns an iterator for generating pseudorandom numbers via a 32-bit [Mersenne Twister][mersenne-twister] pseudorandom number generator.
     36 
     37 ```javascript
     38 var it = iterator();
     39 // returns <Object>
     40 
     41 var r = it.next().value;
     42 // returns <number>
     43 
     44 r = it.next().value;
     45 // returns <number>
     46 
     47 r = it.next().value;
     48 // returns <number>
     49 
     50 // ...
     51 ```
     52 
     53 The function accepts the following `options`:
     54 
     55 -   **normalized**: `boolean` indicating whether to return pseudorandom numbers on the interval `[0,1)`.
     56 -   **seed**: pseudorandom number generator seed.
     57 -   **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
     58 -   **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 iterator has exclusive control over its internal pseudorandom number generator state. Default: `true`.
     59 -   **iter**: number of iterations.
     60 
     61 To return pseudorandom numbers on the interval `[0,1)`, set the `normalized` option.
     62 
     63 ```javascript
     64 var it = iterator({
     65     'normalized': true
     66 });
     67 
     68 var r = it.next().value;
     69 // returns <number>
     70 ```
     71 
     72 To return an iterator having a specific initial state, set the iterator `state` option.
     73 
     74 ```javascript
     75 var bool;
     76 var it1;
     77 var it2;
     78 var r;
     79 var i;
     80 
     81 it1 = iterator();
     82 
     83 // Generate pseudorandom numbers, thus progressing the generator state:
     84 for ( i = 0; i < 1000; i++ ) {
     85     r = it1.next().value;
     86 }
     87 
     88 // Create a new iterator initialized to the current state of `it1`:
     89 it2 = iterator({
     90     'state': it1.state
     91 });
     92 
     93 // Test that the generated pseudorandom numbers are the same:
     94 bool = ( it1.next().value === it2.next().value );
     95 // returns true
     96 ```
     97 
     98 To seed the iterator, set the `seed` option.
     99 
    100 ```javascript
    101 var it = iterator({
    102     'seed': 12345
    103 });
    104 
    105 var r = it.next().value;
    106 // returns 3992670690
    107 
    108 it = iterator({
    109     'seed': 12345
    110 });
    111 
    112 r = it.next().value;
    113 // returns 3992670690
    114 ```
    115 
    116 To limit the number of iterations, set the `iter` option.
    117 
    118 ```javascript
    119 var it = iterator({
    120     'iter': 2
    121 });
    122 
    123 var r = it.next().value;
    124 // returns <number>
    125 
    126 r = it.next().value;
    127 // returns <number>
    128 
    129 r = it.next().done;
    130 // returns true
    131 ```
    132 
    133 The returned iterator protocol-compliant object has the following properties:
    134 
    135 -   **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
    136 -   **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
    137 -   **seed**: pseudorandom number generator seed.
    138 -   **seedLength**: length of generator seed.
    139 -   **state**: writable property for getting and setting the generator state.
    140 -   **stateLength**: length of generator state.
    141 -   **byteLength**: size (in bytes) of generator state.
    142 
    143 </section>
    144 
    145 <!-- /.usage -->
    146 
    147 <section class="notes">
    148 
    149 ## Notes
    150 
    151 -   If an environment supports `Symbol.iterator`, the returned iterator is iterable.
    152 -   [Mersenne Twister][mersenne-twister] 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.
    153 -   The PRNG has a period of `2^19937 - 1`.
    154 -   If PRNG state is "shared" (meaning a state array was provided during iterator creation and **not** copied) and one sets the underlying generator state to a state array having a different length, the iterator does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize the output of the underlying generator according to the new shared state array, the state array for **each** relevant iterator and/or PRNG must be **explicitly** set.
    155 -   If PRNG state is "shared" and one sets the underlying generator state to a state array of the same length, the PRNG state is updated (along with the state of all other iterator and/or PRNGs sharing the PRNG's state array).
    156 
    157 </section>
    158 
    159 <!-- /.notes -->
    160 
    161 <section class="examples">
    162 
    163 ## Examples
    164 
    165 <!-- eslint no-undef: "error" -->
    166 
    167 ```javascript
    168 var iterator = require( '@stdlib/random/iter/mt19937' );
    169 
    170 var it;
    171 var r;
    172 
    173 // Create a seeded iterator for generating pseudorandom numbers:
    174 it = iterator({
    175     'seed': 1234,
    176     'iter': 10
    177 });
    178 
    179 // Perform manual iteration...
    180 while ( true ) {
    181     r = it.next();
    182     if ( r.done ) {
    183         break;
    184     }
    185     console.log( r.value );
    186 }
    187 ```
    188 
    189 </section>
    190 
    191 <!-- /.examples -->
    192 
    193 * * *
    194 
    195 <section class="references">
    196 
    197 ## References
    198 
    199 -   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].
    200 -   Harase, Shin. 2017. "Conversion of Mersenne Twister to double-precision floating-point numbers." _ArXiv_ abs/1708.06018 (September). <https://arxiv.org/abs/1708.06018>.
    201 
    202 </section>
    203 
    204 <!-- /.references -->
    205 
    206 <section class="links">
    207 
    208 [mersenne-twister]: https://en.wikipedia.org/wiki/Mersenne_Twister
    209 
    210 [@matsumoto:1998a]: https://doi.org/10.1145/272991.272995
    211 
    212 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32
    213 
    214 </section>
    215 
    216 <!-- /.links -->