time-to-botec

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

main.js (3506B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var factory = require( './factory.js' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Generates a standard normally distributed random number.
     30 *
     31 * ## Method
     32 *
     33 * -   Given two independent uniformly distributed random variables \\( U_1 \\) and \\( U_2 \\) in the interval \\( [0,1) \\), let
     34 *
     35 *     ``` tex
     36 *     \begin{align*}
     37 *     Z_1 &= R \cos(\theta) = \sqrt{-2 \ln(U_1)} \cos(2\pi U_2) \\
     38 *     Z_2 &= R \sin(\theta) = \sqrt{-2 \ln(U_1)} \sin(2\pi U_2)
     39 *     \end{align*}
     40 *     ```
     41 *
     42 *     where \\( Z_1 \\) and \\( Z_2 \\) are independent random variables with a standard normal distribution.
     43 *
     44 * -   As two uniform random variates are mapped to two standard normal random variates, one of the random variates is cached and returned upon the following invocation.
     45 *
     46 *
     47 * ## Notes
     48 *
     49 * -   The minimum and maximum pseudorandom numbers which can be generated are dependent on the number of bits an underlying uniform pseudorandom number generator (PRNG) uses. For instance, if a PRNG uses \\( 32 \\) bits, the smallest non-zero number that can be generated is \\( 2^{-32}). When \\( U_1 \\) equals this value and \\( U_2 \\) equals \\( 0 \\),
     50 *
     51 *     ``` tex
     52 *     r = \sqrt{-2\ln(2^{-32})} \cos(2\pi) \approx 6.66
     53 *     ```
     54 *
     55 *     which means that the algorithm cannot produce random variates more than \\( 6.66 \\) standard deviations from the mean.
     56 *
     57 *     <!-- <note> -->
     58 *
     59 *     This corresponds to a \\( 2.74 \times 10^{-11} \\) loss due to tail truncation.
     60 *
     61 *     <!-- </note> -->
     62 *
     63 *
     64 * ## References
     65 *
     66 * -   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](http://dx.doi.org/10.1214/aoms/1177706645).
     67 * -   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](http://dx.doi.org/10.1145/363397.363547).
     68 * -   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](http://dx.doi.org/10.1145/362946.362996).
     69 * -   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](http://dx.doi.org/10.1137/1006063).
     70 * -   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](http://dx.doi.org/10.1145/1287620.1287622).
     71 *
     72 *
     73 * @name randn
     74 * @type {PRNG}
     75 * @returns {number} pseudorandom number
     76 *
     77 * @example
     78 * var r = randn();
     79 * // returns <number>
     80 */
     81 var randn = factory();
     82 
     83 
     84 // EXPORTS //
     85 
     86 module.exports = randn;