Last active
November 15, 2016 00:15
-
-
Save juliovedovatto/ee2b1578edc303c08a36a3505961522a to your computer and use it in GitHub Desktop.
random_number: javascript function to sort a number in a given range. Supports blacklisting array of numbers.
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
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