davidbau / seedrandom

seeded random number generator for Javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

global undefined

daurnimator opened this issue · comments

You define the global variable as this.

var global = this,

This doesn't work in many contexts (particularly if inside of a "use strict"); and infact makes your use of global uninferable by JS tooling.

Can one not just initialise seedrandom with a seed to avoid it calling its own internal autoseed()?

e.g.

const width = 256;
const pool = [];

function tostring(a) {
  return String.fromCharCode.apply(0, a);
}

function myAutoseed() {
  try {
    const out = new Uint8Array(width);
    (global.crypto || global.msCrypto).getRandomValues(out);
    return tostring(out);
  } catch (e) {
    const browser = global.navigator;
    const plugins = browser && browser.plugins;
    return [+new Date(), global, plugins, global.screen, tostring(pool)];
  }
}

function main(){
// ...
const rng = seedrandom(myAutoseed());
// ...
}

Released 2.4.4 incorporating pull #46 which should address the strict mode issue.