Created
September 20, 2012 03:59
-
-
Save taf2/3753902 to your computer and use it in GitHub Desktop.
generate random numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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