davidbau / seedrandom

seeded random number generator for Javascript
2.04k stars 160 forks source link

global undefined #47

Closed daurnimator closed 6 years ago

daurnimator commented 7 years ago

You define the global variable as this. https://github.com/davidbau/seedrandom/blob/616a9504fe360d63515c4a051d6d2f3089004329/seedrandom.js#L29

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.

pgbross commented 6 years ago

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());
// ...
}
davidbau commented 6 years ago

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