I have two different functions, one that generates 5 random cards from value 0-51 (unique), and one function that contain an array containing those 5 cards, and an array that contains some of the numbers from array #1 that would be stored.
The function two function two is to replace new values to array #1 if the value is not in array #2. It seems to be something wrong here. after generating numbers for a bit i got:
array(27,18,37,27,45)
returned from the newCards function.
Question: How can i fix newCards function 100% to do what it is supposed to do? (aka use first array, check if number is in 2nd array, if not, make unique here too) since here it went something wrong since it returned two of the same numbers.
code:
function UniqueCards() {
$result = array();
while(count($result) != 5) {
$rand = rand(0,51); // reads as 0 = 1, and 51 = 52. aka starts at zero.
if(!in_array($rand,$result)){
$result[] = $rand;
}
}
return $result;
}
function newCards($input,$exclude) {
$i = 0;
$output = array();
while($i < count($input)) {
$rand = rand(0,51);
if(in_array($input[$i], $exclude)) {
$output[$i] = $input[$i];
$i++;
}
else if(!in_array($rand, $input)){
$output[$i] = $rand;
$i++;
}
}
return $output;
}
Aucun commentaire:
Enregistrer un commentaire