rng-browser.js 735 B

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