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