Using PHP (other languages, using common built-ins are welcome), how can I get a random (or pseudo-random) number that does NOT match a certain criteria?
IE: I want $x = rand(0, 99)
but only if ($x % 9) != 0
.
What is a clean acceptable way to constrain random numbers to a criteria like that?
As an example, using a while()
loop, and possibly break
ing when we have something that meets our criteria, then using the random number after the loop?:
while ( ($rand_key = array_rand($array_of_items)) && ($rand_key % 9 == 0) )
{
// Do nothing?
}
Or something like:
while ( $rand_key = array_rand($array_of_items) )
{
if ( $rand_key % 9 == 0 ) {
break;
}
}
Or is there a more concise or appropriate way to accomplish this?