UNPKG

735 BJavaScriptView Raw
1
2var rng;
3
4var crypto = global.crypto || global.msCrypto; // for IE 11
5if (crypto && crypto.getRandomValues) {
6 // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
7 // Moderately fast, high quality
8 var _rnds8 = new Uint8Array(16);
9 rng = function whatwgRNG() {
10 crypto.getRandomValues(_rnds8);
11 return _rnds8;
12 };
13}
14
15if (!rng) {
16 // Math.random()-based (RNG)
17 //
18 // If all else fails, use Math.random(). It's fast, but is of unspecified
19 // quality.
20 var _rnds = new Array(16);
21 rng = function() {
22 for (var i = 0, r; i < 16; i++) {
23 if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
24 _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
25 }
26
27 return _rnds;
28 };
29}
30
31module.exports = rng;
32