JSFiddle - React, Tailwind, and code Playground
PRNG in Javascript securely seeded by RANDOM.org
by phyreman
HTML
<script src="https://rawgit.com/rubycon/isaac.js/master/isaac.js"></script>
<ul id="log"></ul>
CSS
ul {
list-style: none;
}
JavaScript
function log(m) {
var i = document.createElement('li');
i.innerHTML = m;
document.getElementById('log').appendChild(i);
}
function count(arr) {
var counts = {}, num, len = arr.length, i = 0;
for(; i< len; i++) {
num = arr[i];
counts[num] = counts[num] ? counts[num]+1 : 1;
}
return counts;
}
// A truly random test seed from RANDOM.org to do tests
isaac.seed("pzPTDKt8A4WniEbJa3PQtZzPCLAV4881dUrtiV0=");
// We're gonna send isaac on a short run so we can get some better numbers
isaac.prng(256);
/* Now call isaac.rand() * MAX|0 where "MAX" is whatever number is desired, exclusive, then floor it.*/
/*
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
// Value comes as a single column of digits, so we remove the whitespace
var res = req.responseText;
log(res);
log(res.result.random.data);
//isaac.seed(res);
} else if (req.status === 503) {
// Out of random bits
log('Out of free bits from RANDOM.org');
// check quota
} else {
log('ERROR:\n' + req.responseText);
}
}
}
// We fetch 232-bit BLOB and seed it into ISAAC. This causes a slight bias, but even then it's non-linear and seems random in itself
req.open('POST', 'https://api.random.org/json-rpc/1/invoke', true);
req.setRequestHeader("Content-type", "application/json");
// value of "id" can be anything, it's just used for telling requests apart
req.send(JSON.stringify({"jsonrpc":"2.0","method":"generateBlobs","params":{"apiKey":"2974eb46-e0c4-4950-b577-f38ef7f4ca0a","n":1,"size":232,"format":"base64"},"id":23651}));
*/