Socialify

Folder ..

Viewing mersenne.js
30 lines (27 loc) • 755.0 B

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var Gen = require("../vendor/mersenne").MersenneTwister19937;

function Mersenne() {
  var gen = new Gen();
  gen.init_genrand(new Date().getTime() % 1000000000);

  this.rand = function (max, min) {
    if (max === undefined) {
      min = 0;
      max = 32768;
    }
    return Math.floor(gen.genrand_real2() * (max - min) + min);
  };
  this.seed = function (S) {
    if (typeof S != "number") {
      throw new Error("seed(S) must take numeric argument; is " + typeof S);
    }
    gen.init_genrand(S);
  };
  this.seed_array = function (A) {
    if (typeof A != "object") {
      throw new Error(
        "seed_array(A) must take array of numbers; is " + typeof A
      );
    }
    gen.init_by_array(A, A.length);
  };
}

module.exports = Mersenne;