master
Raw Download raw file
  1'use strict';
  2
  3var width = 256;// each RC4 output is 0 <= x < 256
  4var chunks = 6;// at least six RC4 outputs for each double
  5var digits = 52;// there are 52 significant digits in a double
  6var pool = [];// pool: entropy pool starts empty
  7var GLOBAL = typeof global === 'undefined' ? window : global;
  8
  9//
 10// The following constants are related to IEEE 754 limits.
 11//
 12var startdenom = Math.pow(width, chunks),
 13    significance = Math.pow(2, digits),
 14    overflow = significance * 2,
 15    mask = width - 1;
 16
 17
 18var oldRandom = Math.random;
 19
 20//
 21// seedrandom()
 22// This is the seedrandom function described above.
 23//
 24module.exports = function(seed, options) {
 25  if (options && options.global === true) {
 26    options.global = false;
 27    Math.random = module.exports(seed, options);
 28    options.global = true;
 29    return Math.random;
 30  }
 31  var use_entropy = (options && options.entropy) || false;
 32  var key = [];
 33
 34  // Flatten the seed string or build one from local entropy if needed.
 35  var shortseed = mixkey(flatten(
 36    use_entropy ? [seed, tostring(pool)] :
 37    0 in arguments ? seed : autoseed(), 3), key);
 38
 39  // Use the seed to initialize an ARC4 generator.
 40  var arc4 = new ARC4(key);
 41
 42  // Mix the randomness into accumulated entropy.
 43  mixkey(tostring(arc4.S), pool);
 44
 45  // Override Math.random
 46
 47  // This function returns a random double in [0, 1) that contains
 48  // randomness in every bit of the mantissa of the IEEE 754 value.
 49
 50  return function() {         // Closure to return a random double:
 51    var n = arc4.g(chunks),             // Start with a numerator n < 2 ^ 48
 52        d = startdenom,                 //   and denominator d = 2 ^ 48.
 53        x = 0;                          //   and no 'extra last byte'.
 54    while (n < significance) {          // Fill up all significant digits by
 55      n = (n + x) * width;              //   shifting numerator and
 56      d *= width;                       //   denominator and generating a
 57      x = arc4.g(1);                    //   new least-significant-byte.
 58    }
 59    while (n >= overflow) {             // To avoid rounding up, before adding
 60      n /= 2;                           //   last byte, shift everything
 61      d /= 2;                           //   right using integer Math until
 62      x >>>= 1;                         //   we have exactly the desired bits.
 63    }
 64    return (n + x) / d;                 // Form the number within [0, 1).
 65  };
 66};
 67
 68module.exports.resetGlobal = function () {
 69  Math.random = oldRandom;
 70};
 71
 72//
 73// ARC4
 74//
 75// An ARC4 implementation.  The constructor takes a key in the form of
 76// an array of at most (width) integers that should be 0 <= x < (width).
 77//
 78// The g(count) method returns a pseudorandom integer that concatenates
 79// the next (count) outputs from ARC4.  Its return value is a number x
 80// that is in the range 0 <= x < (width ^ count).
 81//
 82/** @constructor */
 83function ARC4(key) {
 84  var t, keylen = key.length,
 85      me = this, i = 0, j = me.i = me.j = 0, s = me.S = [];
 86
 87  // The empty key [] is treated as [0].
 88  if (!keylen) { key = [keylen++]; }
 89
 90  // Set up S using the standard key scheduling algorithm.
 91  while (i < width) {
 92    s[i] = i++;
 93  }
 94  for (i = 0; i < width; i++) {
 95    s[i] = s[j = mask & (j + key[i % keylen] + (t = s[i]))];
 96    s[j] = t;
 97  }
 98
 99  // The "g" method returns the next (count) outputs as one number.
100  (me.g = function(count) {
101    // Using instance members instead of closure state nearly doubles speed.
102    var t, r = 0,
103        i = me.i, j = me.j, s = me.S;
104    while (count--) {
105      t = s[i = mask & (i + 1)];
106      r = r * width + s[mask & ((s[i] = s[j = mask & (j + t)]) + (s[j] = t))];
107    }
108    me.i = i; me.j = j;
109    return r;
110    // For robust unpredictability discard an initial batch of values.
111    // See http://www.rsa.com/rsalabs/node.asp?id=2009
112  })(width);
113}
114
115//
116// flatten()
117// Converts an object tree to nested arrays of strings.
118//
119function flatten(obj, depth) {
120  var result = [], typ = (typeof obj)[0], prop;
121  if (depth && typ == 'o') {
122    for (prop in obj) {
123      try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {}
124    }
125  }
126  return (result.length ? result : typ == 's' ? obj : obj + '\0');
127}
128
129//
130// mixkey()
131// Mixes a string seed into a key that is an array of integers, and
132// returns a shortened string seed that is equivalent to the result key.
133//
134function mixkey(seed, key) {
135  var stringseed = seed + '', smear, j = 0;
136  while (j < stringseed.length) {
137    key[mask & j] =
138      mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++));
139  }
140  return tostring(key);
141}
142
143//
144// autoseed()
145// Returns an object for autoseeding, using window.crypto if available.
146//
147/** @param {Uint8Array=} seed */
148function autoseed(seed) {
149  try {
150    GLOBAL.crypto.getRandomValues(seed = new Uint8Array(width));
151    return tostring(seed);
152  } catch (e) {
153    return [+new Date, GLOBAL, GLOBAL.navigator && GLOBAL.navigator.plugins,
154            GLOBAL.screen, tostring(pool)];
155  }
156}
157
158//
159// tostring()
160// Converts an array of charcodes to a string
161//
162function tostring(a) {
163  return String.fromCharCode.apply(0, a);
164}
165
166//
167// When seedrandom.js is loaded, we immediately mix a few bits
168// from the built-in RNG into the entropy pool.  Because we do
169// not want to intefere with determinstic PRNG state later,
170// seedrandom will not call Math.random on its own again after
171// initialization.
172//
173mixkey(Math.random(), pool);