time-to-botec

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

README.md (7271B)


      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 > Uniformly distributed pseudorandom numbers between 0 and 1.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var randu = require( '@stdlib/random/base/randu' );
     31 ```
     32 
     33 #### randu()
     34 
     35 Returns a uniformly distributed pseudorandom number on the interval `[0,1)`.
     36 
     37 ```javascript
     38 var r = randu();
     39 // returns <number>
     40 ```
     41 
     42 #### randu.factory( \[options] )
     43 
     44 Returns a pseudorandom number generator (PRNG) for generating uniformly distributed random numbers on the interval `[0,1)`.
     45 
     46 ```javascript
     47 var rand = randu.factory();
     48 ```
     49 
     50 The function accepts the following `options`:
     51 
     52 -   **name**: name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers. The following generators are supported:
     53 
     54     -   [`mt19937`][@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/mt19937
     55     -   [`minstd`][@stdlib/random/base/minstd]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd
     56     -   [`minstd-shuffle`][@stdlib/random/base/minstd-shuffle]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd-shuffle
     57 
     58     Default: [`'mt19937'`][@stdlib/random/base/mt19937].
     59 
     60 -   **seed**: pseudorandom number generator seed. Valid seed values vary according to the underlying PRNG.
     61 
     62 -   **state**: pseudorandom number generator state. Valid state values vary according to the underlying PRNG. If provided, the function ignores the `seed` option.
     63 
     64 -   **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 generator has exclusive control over its internal state. Default: `true`.
     65 
     66 By default, the underlying pseudorandom number generator is [`mt19937`][@stdlib/random/base/mt19937]. To use a different PRNG, set the `name` option.
     67 
     68 ```javascript
     69 var rand = randu.factory({
     70     'name': 'minstd-shuffle'
     71 });
     72 
     73 var r = rand();
     74 // returns <number>
     75 ```
     76 
     77 To seed a pseudorandom number generator, set the `seed` option.
     78 
     79 ```javascript
     80 var rand1 = randu.factory({
     81     'seed': 12345
     82 });
     83 
     84 var r1 = rand1();
     85 // returns <number>
     86 
     87 var rand2 = randu.factory({
     88     'seed': 12345
     89 });
     90 
     91 var r2 = rand2();
     92 // returns <number>
     93 
     94 var bool = ( r1 === r2 );
     95 // returns true
     96 ```
     97 
     98 To return a generator having a specific initial state, set the generator `state` option.
     99 
    100 ```javascript
    101 var rand;
    102 var bool;
    103 var r;
    104 var i;
    105 
    106 // Generate pseudorandom numbers, thus progressing the generator state:
    107 for ( i = 0; i < 1000; i++ ) {
    108     r = randu();
    109 }
    110 
    111 // Create a new PRNG initialized to the current state of `randu`:
    112 rand = randu.factory({
    113     'state': randu.state
    114 });
    115 
    116 // Test that the generated pseudorandom numbers are the same:
    117 bool = ( rand() === randu() );
    118 // returns true
    119 ```
    120 
    121 #### randu.NAME
    122 
    123 The generator name.
    124 
    125 ```javascript
    126 var str = randu.NAME;
    127 // returns 'randu'
    128 ```
    129 
    130 #### randu.PRNG
    131 
    132 The underlying pseudorandom number generator.
    133 
    134 ```javascript
    135 var prng = randu.PRNG;
    136 // returns <Function>
    137 ```
    138 
    139 #### randu.MIN
    140 
    141 Minimum value lower bound.
    142 
    143 ```javascript
    144 var min = randu.MIN;
    145 // returns 0.0
    146 ```
    147 
    148 #### randu.MAX
    149 
    150 Maximum value upper bound (not greater than `1` exclusive).
    151 
    152 ```javascript
    153 var max = randu.MAX;
    154 // returns <number>
    155 ```
    156 
    157 #### randu.seed
    158 
    159 The value used to seed `randu()`.
    160 
    161 ```javascript
    162 var rand;
    163 var r;
    164 var i;
    165 
    166 // Generate pseudorandom values...
    167 for ( i = 0; i < 100; i++ ) {
    168     r = randu();
    169 }
    170 
    171 // Generate the same pseudorandom values...
    172 rand = randu.factory({
    173     'seed': randu.seed
    174 });
    175 for ( i = 0; i < 100; i++ ) {
    176     r = rand();
    177 }
    178 ```
    179 
    180 #### randu.seedLength
    181 
    182 Length of generator seed.
    183 
    184 ```javascript
    185 var len = randu.seedLength;
    186 // returns <number>
    187 ```
    188 
    189 #### randu.state
    190 
    191 Writable property for getting and setting the generator state.
    192 
    193 ```javascript
    194 var r = randu();
    195 // returns <number>
    196 
    197 r = randu();
    198 // returns <number>
    199 
    200 // ...
    201 
    202 // Get a copy of the current state:
    203 var state = randu.state;
    204 
    205 r = randu();
    206 // returns <number>
    207 
    208 r = randu();
    209 // returns <number>
    210 
    211 // Reset the state:
    212 randu.state = state;
    213 
    214 // Replay the last two pseudorandom numbers:
    215 r = randu();
    216 // returns <number>
    217 
    218 r = randu();
    219 // returns <number>
    220 
    221 // ...
    222 ```
    223 
    224 #### randu.stateLength
    225 
    226 Length of generator state.
    227 
    228 ```javascript
    229 var len = randu.stateLength;
    230 // returns <number>
    231 ```
    232 
    233 #### randu.byteLength
    234 
    235 Size (in bytes) of generator state.
    236 
    237 ```javascript
    238 var sz = randu.byteLength;
    239 // returns <number>
    240 ```
    241 
    242 #### randu.toJSON()
    243 
    244 Serializes the pseudorandom number generator as a JSON object.
    245 
    246 ```javascript
    247 var o = randu.toJSON();
    248 // returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] }
    249 ```
    250 
    251 </section>
    252 
    253 <!-- /.usage -->
    254 
    255 <section class="notes">
    256 
    257 ## Notes
    258 
    259 -   **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.
    260 -   If PRNG state is "shared" (meaning a state array was provided during PRNG creation and **not** copied) and one sets the generator state to a state array having a different length, the 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.
    261 -   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).
    262 
    263 </section>
    264 
    265 <!-- /.notes -->
    266 
    267 <section class="examples">
    268 
    269 ## Examples
    270 
    271 <!-- eslint no-undef: "error" -->
    272 
    273 ```javascript
    274 var randu = require( '@stdlib/random/base/randu' );
    275 
    276 var seed;
    277 var rand;
    278 var i;
    279 
    280 // Generate pseudorandom numbers...
    281 for ( i = 0; i < 100; i++ ) {
    282     console.log( randu() );
    283 }
    284 
    285 // Create a new pseudorandom number generator...
    286 seed = 1234;
    287 rand = randu.factory({
    288     'seed': seed
    289 });
    290 for ( i = 0; i < 100; i++ ) {
    291     console.log( rand() );
    292 }
    293 
    294 // Create another pseudorandom number generator using a previous seed...
    295 rand = randu.factory({
    296     'seed': randu.seed
    297 });
    298 for ( i = 0; i < 100; i++ ) {
    299     console.log( rand() );
    300 }
    301 ```
    302 
    303 </section>
    304 
    305 <!-- /.examples -->
    306 
    307 <section class="links">
    308 
    309 [@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/mt19937
    310 
    311 [@stdlib/random/base/minstd]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd
    312 
    313 [@stdlib/random/base/minstd-shuffle]: https://www.npmjs.com/package/@stdlib/random/tree/main/base/minstd-shuffle
    314 
    315 </section>
    316 
    317 <!-- /.links -->