Random Normal Distribution

Benchmark created by David Hogg on


Description

Tests to compare performance of various methods to generate random numbers according to a Normal Distribution.

Setup

function randBoxMuller() {
      var u, v, a, b, x, y;
    
      u = Math.random();
      v = Math.random();
    
      a = Math.sqrt(-2 * Math.log(u));
      b = 2 * Math.PI * v;
    
      x = a * Math.cos(b);
      y = a * Math.sin(b);
    
      return [x, y];
    }
    
    function randMarsaglia() {
      var u, v, s, a, x, y;
      do {
        s = u * u + v * v;
      } while (s >= 1)
      a = Math.sqrt(-2 * Math.log(s) / s);
    
      x = u * a;
      y = u * a;
    
      return [x, y];
    }
    
    function randApproximation() {
      var x, y;
      x = (Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) / 6;
      y = (Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) / 6;
    
      return [x, y]
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Box-Muller Method
randBoxMuller();
ready
Marsaglia Polar Method
randMarsaglia();
ready
Approximation Method
randApproximation()
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by David Hogg on
  • Revision 2: published by David Hogg on
  • Revision 3: published by David Hogg on
  • Revision 4: published on