Skip to content

Instantly share code, notes, and snippets.

@mach3
Created August 27, 2014 09:17
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 mach3/59ae555c7f276145db5d to your computer and use it in GitHub Desktop.
Save mach3/59ae555c7f276145db5d to your computer and use it in GitHub Desktop.
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