Skip to content

Instantly share code, notes, and snippets.

@rhengles
Created November 27, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhengles/9208e61dde3062e26cb7 to your computer and use it in GitHub Desktop.
Save rhengles/9208e61dde3062e26cb7 to your computer and use it in GitHub Desktop.
setTimeout-based entropy
// please forgive the one letter variables, this is a gist
// the setTimeout must be called sequentially, or else the
// runtime will execute all of them at the same time,
// giving the same value
function random(m, cb) {
var t = Date.now();
setTimeout(function() {
cb(Date.now()-t);
}, m);
}
function randomTimes(x, m, cb) {
function next() {
if ( i == x ) return cb(a);
random(m, function(v) {
a[i] = v;
i++;
next();
});
}
var a = [];
var i = 0;
next();
}
// example
randomTimes(20, 0, function(a) { console.log(a); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment