3

I am trying to find a solution in PHP that can generate three random numbers. At the moment I have this that generates a random number that is different from $randNum; The numbers need to be different from each other and also different from the variable $randNum

Thank you

    $wrong = $randNum;
    while ($wrong == $randNum) {
        $wrong = rand(0,$max - 1);

    }

1 Answers1

2
<?php

$numbers = [];

for($i = 0; $i < 10; $i++){
    $number = rand (1,15);
    while (in_array($number, $numbers)){
        $number = rand (1,15);
    }
    $numbers[] = $number;
}


echo '<pre>';
print_r($numbers);
echo '</pre>';

This function generates unique 10 random numbers, range from 1-15 you can easy change this script to your needs.

fico7489
  • 7,931
  • 7
  • 55
  • 89