UNPKG

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