As far as I know, it's not possible to generate a random number and take into account an exclusion set. You need to create a recursive function to do the filtering for you. See below:
function GenerateRandomNumber() {
var min = -13, max = 13;
var random = Math.floor(Math.random() * (max - min + 1)) + min;
// If random number is undesirable, generate a new number, else return
return (random < 4 && random > -4) ? GenerateRandomNumber() : random;
}
var myRandomNumber = GenerateRandomNumber();
Here's a working fiddle.
Or, courtesy @Rozwel, a non-recursive version using a while loop:
function GenerateRandomNumber() {
var min = -13, max = 13;
var random = 0;
while (random < 4 && random > -4) {
random = Math.floor(Math.random() * (max - min + 1)) + min;
}
return random;
}
Here's a jsperf that compares the accepted answer with these two options.