Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
random_number: javascript function to sort a number in a given range. Supports blacklisting array of numbers.
function random_number(min, max, blacklist) {
min = min || 0;
max = max || 0;
var random = Math.floor(Math.random() * (max - min + 1)) + min;
if (!blacklist || blacklist.constructor !== Array)
return random;
else if (typeof blacklist == 'string')
blacklist = [ blacklist ];
return blacklist.filter(function(number) { return number == random; }).lenght ? random_number(min, max, blacklist) : random;
}
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.