Skip to content

Instantly share code, notes, and snippets.

@mach3 mach3/lottery.js
Created Aug 27, 2014

Embed
What would you like to do?
var lottery = function(data, rate, times){
var source = [];
rate = rate || (function(){
var i, rate;
i = data.length;
rate = [];
while(i--){
rate.push(1);
}
return rate;
}());
times = times || 10;
data.forEach(function(value, i){
var len = rate[i] * times;
while(len--){
source.push(value);
}
});
source.sort(function(){
return 0.5 - Math.random();
});
return source[parseInt(Math.random() * source.length, 10)];
};
// Test
lottery(["foo", "bar", "baz"], [1, 8, 16], 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.