Skip to content

Instantly share code, notes, and snippets.

@taf2
Created September 20, 2012 03:59
Show Gist options
  • Save taf2/3753902 to your computer and use it in GitHub Desktop.
Save taf2/3753902 to your computer and use it in GitHub Desktop.
generate random numbers
function getRandom(cb) {
var now = new Date().getTime() - (Math.random(10)*1000);
setTimeout(function() {
var then = new Date().getTime() * 1000;
cb((then-now)/Math.random(100));
}, Math.random(90));
}
var randomSet = {}
var collisionCount = 0;
var count = 0;
var trials = 100001;
for (var i = 0; i < trials; ++i) {
getRandom(function(rn) {
console.log(rn);
++count;
if (randomSet[rn]) {
++collisionCount;
}
randomSet[rn] = 1;
if (count % 1000 == 0 || count == trials) {
console.log("processed: " + count + " with: " + collisionCount + " collisions");
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment