PHP excluding numbers from rand () function

I run rand () 3 times, and I want to exclude the first two results from the features of the function. As if it hit 1 and 5, I want the next rand () to exclude 1 and 5 from its range. How should I do it?

+3
source share
3 answers

What about:

do {   
    $rand_number = rand();

}while(in_array($rand_number, array(1,5));
+5
source

If you want to create three unique random numbers (ish), you can use:

$totalNumsNeeded = 3;
$randoms = array();
while (count($randoms) < $totalNumsNeeded) {
    $random = rand($min, $max);
    if (!in_array($random, $randoms)) {
        $randoms[] = $random;
    }
}
+1
source
$last[];

for ($i = 0; $i < 10; $i++) {
  $min = getLow($last);
  $max = getHigh($last);

  $myrand =  rand ( $min, $max )
  $last[i] = $myrand;
}

$ , ... , $min $max . .

$last;
While (true) {
  $myRand = rand();
  if ($myRand != $last) {
    $last = $myRand;
    break;
  }

}
0

Source: https://habr.com/ru/post/1795166/


All Articles