rng-browser.js 682 B

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