time-to-botec

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

README.md (7685B)


      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 # Standard Normal Random Numbers
     22 
     23 > Create an iterator for generating pseudorandom numbers drawn from a [standard normal][normal] distribution using the [Box-Muller transform][box-muller].
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var iterator = require( '@stdlib/random/iter/box-muller' );
     31 ```
     32 
     33 #### iterator( \[options] )
     34 
     35 Returns an iterator for generating pseudorandom numbers drawn from a [standard normal][normal] distribution using the [Box-Muller transform][box-muller].
     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 -   **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).
     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 use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option.
     62 
     63 ```javascript
     64 var minstd = require( '@stdlib/random/base/minstd' );
     65 
     66 var it = iterator({
     67     'prng': minstd.normalized
     68 });
     69 
     70 var r = it.next().value;
     71 // returns <number>
     72 ```
     73 
     74 To return an iterator having a specific initial state, set the iterator `state` option.
     75 
     76 ```javascript
     77 var bool;
     78 var it1;
     79 var it2;
     80 var r;
     81 var i;
     82 
     83 it1 = iterator();
     84 
     85 // Generate pseudorandom numbers, thus progressing the generator state:
     86 for ( i = 0; i < 1000; i++ ) {
     87     r = it1.next().value;
     88 }
     89 
     90 // Create a new iterator initialized to the current state of `it1`:
     91 it2 = iterator({
     92     'state': it1.state
     93 });
     94 
     95 // Test that the generated pseudorandom numbers are the same:
     96 bool = ( it1.next().value === it2.next().value );
     97 // returns true
     98 ```
     99 
    100 To seed the iterator, set the `seed` option.
    101 
    102 ```javascript
    103 var it = iterator({
    104     'seed': 12345
    105 });
    106 
    107 var r = it.next().value;
    108 // returns ~0.349
    109 
    110 it = iterator({
    111     'seed': 12345
    112 });
    113 
    114 r = it.next().value;
    115 // returns ~0.349
    116 ```
    117 
    118 To limit the number of iterations, set the `iter` option.
    119 
    120 ```javascript
    121 var it = iterator({
    122     'iter': 2
    123 });
    124 
    125 var r = it.next().value;
    126 // returns <number>
    127 
    128 r = it.next().value;
    129 // returns <number>
    130 
    131 r = it.next().done;
    132 // returns true
    133 ```
    134 
    135 The returned iterator protocol-compliant object has the following properties:
    136 
    137 -   **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.
    138 -   **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
    139 -   **seed**: pseudorandom number generator seed. If provided a `prng` option, the property value is `null`.
    140 -   **seedLength**: length of generator seed. If provided a `prng` option, the property value is `null`.
    141 -   **state**: writable property for getting and setting the generator state. If provided a `prng` option, the property value is `null`.
    142 -   **stateLength**: length of generator state. If provided a `prng` option, the property value is `null`.
    143 -   **byteLength**: size (in bytes) of generator state. If provided a `prng` option, the property value is `null`.
    144 -   **PRNG**: underlying pseudorandom number generator.
    145 
    146 </section>
    147 
    148 <!-- /.usage -->
    149 
    150 <section class="notes">
    151 
    152 ## Notes
    153 
    154 -   If an environment supports `Symbol.iterator`, the returned iterator is iterable.
    155 -   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.
    156 -   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).
    157 
    158 </section>
    159 
    160 <!-- /.notes -->
    161 
    162 <section class="examples">
    163 
    164 ## Examples
    165 
    166 <!-- eslint no-undef: "error" -->
    167 
    168 ```javascript
    169 var iterator = require( '@stdlib/random/iter/box-muller' );
    170 
    171 var it;
    172 var r;
    173 
    174 // Create a seeded iterator for generating pseudorandom numbers:
    175 it = iterator({
    176     'seed': 1234,
    177     'iter': 10
    178 });
    179 
    180 // Perform manual iteration...
    181 while ( true ) {
    182     r = it.next();
    183     if ( r.done ) {
    184         break;
    185     }
    186     console.log( r.value );
    187 }
    188 ```
    189 
    190 </section>
    191 
    192 <!-- /.examples -->
    193 
    194 * * *
    195 
    196 <section class="references">
    197 
    198 ## References
    199 
    200 -   Box, G. E. P., and Mervin E. Muller. 1958. "A Note on the Generation of Random Normal Deviates." _The Annals of Mathematical Statistics_ 29 (2). The Institute of Mathematical Statistics: 610–11. doi:[10.1214/aoms/1177706645][@box:1958].
    201 -   Bell, James R. 1968. "Algorithm 334: Normal Random Deviates." _Communications of the ACM_ 11 (7). New York, NY, USA: ACM: 498. doi:[10.1145/363397.363547][@bell:1968].
    202 -   Knop, R. 1969. "Remark on Algorithm 334 \[G5]: Normal Random Deviates." _Communications of the ACM_ 12 (5). New York, NY, USA: ACM: 281. doi:[10.1145/362946.362996][@knop:1969].
    203 -   Marsaglia, G., and T. A. Bray. 1964. "A Convenient Method for Generating Normal Variables." _SIAM Review_ 6 (3). Society for Industrial; Applied Mathematics: 260–64. doi:[10.1137/1006063][@marsaglia:1964a].
    204 -   Thomas, David B., Wayne Luk, Philip H.W. Leong, and John D. Villasenor. 2007. "Gaussian Random Number Generators." _ACM Computing Surveys_ 39 (4). New York, NY, USA: ACM. doi:[10.1145/1287620.1287622][@thomas:2007].
    205 
    206 </section>
    207 
    208 <!-- /.references -->
    209 
    210 <section class="links">
    211 
    212 [box-muller]: https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
    213 
    214 [normal]: https://en.wikipedia.org/wiki/Normal_distribution
    215 
    216 [@box:1958]: http://dx.doi.org/10.1214/aoms/1177706645
    217 
    218 [@bell:1968]: http://dx.doi.org/10.1145/363397.363547
    219 
    220 [@knop:1969]: http://dx.doi.org/10.1145/362946.362996
    221 
    222 [@marsaglia:1964a]: http://dx.doi.org/10.1137/1006063
    223 
    224 [@thomas:2007]: http://dx.doi.org/10.1145/1287620.128762
    225 
    226 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32
    227 
    228 </section>
    229 
    230 <!-- /.links -->