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