/
math-random-polyfill.js
32 lines (31 loc) · 1.09 KB
/
math-random-polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// math-random-polyfill.js
// https://github.com/DavidAnson/math-random-polyfill
// 2016-12-03
(function iife () {
"use strict";
// Feature detection
var crypto = window.crypto || window.msCrypto;
if (window.Uint32Array && crypto && crypto.getRandomValues) {
// Capture functions and values
var Math_random = Math.random.bind(Math);
var crypto_getRandomValues = crypto.getRandomValues.bind(crypto);
var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
var highShift = Math.pow(2, 32);
var highMask = Math.pow(2, 53 - 32) - 1;
// Polyfill Math.random
Math.random = function math_random_polyfill () {
try {
// Get random bits for numerator
var array = new Uint32Array(2);
crypto_getRandomValues(array);
var numerator = ((array[0] & highMask) * highShift) + array[1];
// Divide by maximum-value denominator
var denominator = MAX_SAFE_INTEGER + 1;
return numerator / denominator;
} catch (ex) {
// Exception in crypto.getRandomValues, fall back to Math.random
return Math_random();
}
};
}
}());