Reputation: 1957
I want to get a random number from (0,2,6,8,4) or such ranges. Currently I loop till I get appropriate Num.
boxNum = getRandomNum(0, 8);
while (boxNum == 1 || boxNum == 3 || boxNum == 5 || boxNum == 7))
boxNum = getRandomNum(0, 8);
Is there any other way to do this?? The while loop crashes the page after some turns.
Upvotes: 1
Views: 94
Reputation: 553
Pretty Simple You store the desired value in the array . Get the value from it randomly .
var RangeOfnumber = [0,2,4,6,8];
function getRandomNumber(){
var randomNumGen= RangeOfnumber[Math.floor(Math.random()*RangeOfnumber.length)];
return randomNumGen;
}
Upvotes: 1