# Native and non-native Random Numbers

## JavaScript performance comparison

Test case created by Adam Hyland and last updated

## Info

Comparison of performance for native Math.random(), a seedable random number generator written by David Bau and a simple xorshift generator.

## Preparation code

``<script src="http://davidbau.com/encode/seedrandom-min.js"></script><script>var seed = [  123456789,  362436069,  521288629,  88675123];var denom = Math.pow(2, 32);var xorshiftbare = function() {  // required inputs  var x = seed[0],      w = seed[3];  // derived values  var t = x^(x << 11),      r = w^(w >> 19)^(t^(t >> 8));  // update the seed array  seed = [seed[1], seed[2], w, r];  return r / denom;};var xor = {  seed : [123456789, 362436069, 521288629, 88675123 ],  tuple : [11, 19, 8],  denom : Math.pow(2, 32),  shift : function() {    // inputs from the seed    var x,         w;    // outputs generated from bit shifts    var t,         r;    var arr = this.seed;    var tuple = this.tuple,        a = tuple[0],        b = tuple[1],        c = tuple[2];    x = arr.shift();    t = x^(x << a);    w = arr.slice(-1);    r = w^(w >> b)^(t^(t >> c));    arr.push(r);    this.seed = arr;    return r / this.denom;  }}</script><script>  Benchmark.prototype.setup = function() {    Math.oldrand = Math.random;    Math.seedrandom();      };  Benchmark.prototype.teardown = function() {    Math.random = Math.oldrand;  };</script>``

## Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Native Math.random
``Math.oldrand();``
pending…
Bau's ARC4
``Math.random();``
pending…
Bare XOR-shift
``xorshiftbare();``
pending…
prettier/modular XOR shift
``xor.shift();``
pending…

## Revisions

You can edit these tests or add even more tests to this page by appending `/edit` to the URL. Here’s a list of current revisions for this page: