leoOrion
leoOrion

Reputation: 1957

Obtaining Random Numbers from irregular Ranges

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

Answers (2)

Kartikeya Sharma
Kartikeya Sharma

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

Dave
Dave

Reputation: 483

Hmm... 2 * getRandomNum(0,4) ?

Upvotes: 1

Related Questions