simjs-random

Random number generation utilities from simjs

Usage no npm install needed!

<script type="module">
  import simjsRandom from 'https://cdn.skypack.dev/simjs-random';
</script>

README

<title>SIM.JS | Discrete Event Simulation in JavaScript</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
  var DOCUMENTATION_OPTIONS = {
    URL_ROOT:    '',
    VERSION:     '0.26',
    COLLAPSE_INDEX: false,
    FILE_SUFFIX: '.html',
    HAS_SOURCE:  true
  };
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="" href="index.html" />
<div class="document">
  <div class="documentwrapper">
    <div class="bodywrapper">
      <div class="body">

Random Number Generation

Why not Math.random()?

The JavaScript’s native Math.random() function is not suited for Discrete Event Simulations, since:

  • The Math.random() function cannot be seeded. There is no way to guarantee that the same random stream will be produced the next time the script is executed.
  • There is only one stream of random numbers. Statistics purists frown upon when two independent random distributions are drawn from same seed.
  • The javascript library provides only the uniform probability distribution function. In DES, as also in many other scientific applications, there is a need for other kinds of distributions, such as exponential, gaussian, pareto etc.
  • The native Math.random() does not use (at the time of writing) the arguably better Mersenne Twister algorithm for random number generator (see Mersenne Twister website and Wikipedia article).

The Random Library

The Random library uses the Mersenne Twister algorithm for generating random number stream, and is based on the JavaScript implementation by Makoto Matsumoto and Takuji Nishimura (code).

The original code is wrapped around as a javascript class and there can be multiple objects each representing different random number streams. For example,

/* Demonstrate that random number streams can be seeded,
 * and multiple streams can be created in a single script. */
var stream1 = new Random(1234);
var stream2 = new Random(6789);

stream1.random(); // returns 0.966453535715118 always stream2.random(); // returns 0.13574991398490965 always

In addition, the Random library supports following probability distribution functions:

API Reference

class Random ([seed])
Arguments:
  • seed (integer) – An optional seed value. If this argument is not provided, then the seed value is set to new Date().getTime().
Random.random()

Returns a uniformly generated random floating point number in the range [0, 1.0).

Random.exponential(lambda)

Exponential distribution. lambda is the rate (inverse of mean) for the distribution. lambda is a required parameters, and must be non-negative and non-zero.

Random.gamma(alpha, beta)

Gamma distribution. alpha is sometimes also known as shape of the distribution, while beta as the scale. Both arguments are required.

This function is adapted from Python 2.6 implementation of random.py.

Random.normal(mu, sigma)

Normal (or Gaussian) distribution. mu is the mean of the Gaussian probability density function, and sigma is the standard deviation. Both parameters are required.

Random.pareto(alpha)

Pareto distribution. The alpha parameter is required.

Random.triangular(lower, upper, mode)

Triangular distribution. The random number are generated between the range (lower, upper) with mode as the mode value. All three parameters are required.

Random.uniform(lower, upper)

Uniform distribution. Returns a uniformly generated random number in the range [lower, upper). Both lower and upper arguments are required.

Random.weibull(alpha, beta)

Weibull distribution. Both alpha and beta parameters are required.

      </div>
    </div>
  </div>
  <div class="sphinxsidebar">
    <div class="sphinxsidebarwrapper">

Table Of Contents

Previous topic

Statistics

Next topic

Download SIM.JS, random.js

    </div>
  </div>
  <div class="clearer"></div>
</div>
<div class="related">
  <h3>Navigation</h3>
  <ul>
    <li class="right" style="margin-right: 10px">
      <a href="download.html" title="Download SIM.JS, random.js"
         >next</a></li>
    <li class="right" >
      <a href="statistics.html" title="Statistics"
         >previous</a> |</li>
    <li><a href="index.html">Home</a> &raquo;</li>
  </ul>
</div>

<div class="footer">
    &copy; Copyright 2011, Maneesh Varshney.
  Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</div>