PDA

View Full Version : Noob question about random


Kel
08-05-2005, 11:40 AM
Hi, how can i make a random number not equal another I mean:

Math.round(Math.random()*3+1) ! (lets say 2)

Thank you.

Vector
08-05-2005, 01:24 PM
The quickest way to do something like this would be to have an infinite loop function that keeps on trying until it pulls up a number that is different to something you specify... eg

function fRandom(iRange:Number, iNot:Number) : Number {
var vNum:Number = random(iRange);
fRandom = (vNum != iNot) ? vNum : fRandom(iRange, iNot);
}

iRange is the range of the random numbers you want to pick. iNot is the number that you don't want it to pick.
vNum is the random number that the function picks.
If the random number isn't the number that you want to avoid then return that number. If the random number is the number that you want to avoid, then call the function again. :)