mercredi 22 mars 2017

Regenerate random number in for loop if a certain condition isn't met in PHP

I have this loop:

for ($i = 0; $i < 9; $i++) {
    $random_number = rand(1, 9);

    if (!in_array($section_one, $random_number)) {
        array_push($section_one, rand(1, 9));
    }
}

As you can see the if statement in the loop checks if the generated random number is already in the array called $section_one. The problem is that if the random number is in the array, a new random number should be generated. Doing this ones is easy, but it has to keep regenerating a random number until a unique one has been generated.

How can I do that?




Aucun commentaire:

Enregistrer un commentaire