Created
January 13, 2016 21:23
-
-
Save danieluhl/3ec4dee0405b4bc6b25e to your computer and use it in GitHub Desktop.
not so random node lottery simulator distribution example
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
// somehow when running this in node I get strange numbers for the distribution of the final "random" number | |
// this assumes a lottery system of 6 balls, 5 picked randomly from a batch of 39 and the final one from 8 | |
var fastLottery = function() { | |
var count = 0; | |
var running = true; | |
var distribution = [0, 0, 0, 0, 0, 0, 0, 0]; | |
var finalCount = 0; | |
while (running) { | |
count++; | |
if (Math.floor(Math.random() * 39) + 1 === 39 && | |
Math.floor(Math.random() * 38) + 1 === 38 && | |
Math.floor(Math.random() * 37) + 1 === 37 && | |
Math.floor(Math.random() * 36) + 1 === 36 && | |
Math.floor(Math.random() * 35) + 1 === 35) { | |
var final = Math.floor(Math.random() * 8) + 1; | |
distribution[final - 1]++; | |
// if(final === 4) { | |
// // WINNER! | |
// break; | |
// } | |
finalCount++; | |
if(finalCount > 50) { | |
console.log(distribution); | |
console.log(count); | |
break; | |
} | |
} | |
} | |
return count; | |
}; | |
console.time('lottery time'); | |
fastLottery(); | |
console.timeEnd('lottery time'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment