mardi 19 décembre 2017

Random Numbers without duplication using an array in php

I'm trying to create a random number generator in PHP. It's supposed to generate three (3) numbers at a time, without repeat. That's to say, the 3 numbers cannot be the same.

here's what I've tried so far:

$array = [];

$A = mt_rand(1,36);
$array[0] = $A;

$B = mt_rand(1,36);
$array[1] = $B;

if(in_array($B,$array)){
    $B = mt_rand(1,36);
    $array[1] = $B;
}

$C = mt_rand(1,36);
$array[2] = $C;

if(in_array($C,$array)){
    $C = mt_rand(1,36);
    $array[2] = $C;
}

$length = count($array);

//display the array values;

for($i = 0; $i < $length; $i++){
    echo ($array[$i]."<br>");
}

Can anyone tell me where I'm going wrong?




Aucun commentaire:

Enregistrer un commentaire