I'm running rand() 3 times, and I want to exclude the first two results from the possibilities of the function. Like if it hit 1 and 5, I want the next rand() to exclude 1 and 5 from its range. How would I do this?
How about:
do {
$rand_number = rand();
}while(in_array($rand_number, array(1,5));
If you want to generate three unique random(ish) numbers, you could use:
$totalNumsNeeded = 3;
$randoms = array();
while (count($randoms) < $totalNumsNeeded) {
$random = rand($min, $max);
if (!in_array($random, $randoms)) {
$randoms[] = $random;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With