3
How do I get all 60 numbers?
It’s already coming randomly but only one number.
<?php
class CodeGen {
private $codes = array();
public function __construct($codes) {
$this->codes = $codes;
}
public function getRandomCode($min, $max){
$next = count($this->codes) + 1;
while (count($this->codes) < $next) {
$code = mt_rand($min, $max);
if (!in_array($code, $this->codes)) {
$this->codes[] = $code;
}
}
}
public function getLastCode(){
return end($this->codes);
}
}
$codes = array();
$CodeGen = new CodeGen($codes);
$CodeGen->getRandomCode(0, 60);
print $CodeGen->getLastCode();
?>
Within your
while (count($this->codes) < $next) { ... }
is not increasing the value of$next
. Otherwise, the condition will beFALSE
and will come out of loop, resulting in only one number. What defines how many times thiswhile
should perform? For with this condition it will possibly generate loop infinity when it works.– user98628
Create another method or remove
end()
ingetLastCode
– rray
OK, I’ll do it thank you very much
– Bruna
tested but did not work
– Bruna
Your
while
is generating only 1 number. You need to change the condition that is being validated. Run:var_dump($CodeGen)
, you will see that propertycodes
will only have 1 number.– user98628
I understand thank you very much
– Bruna