How do I get all 60 numbers

Asked

Viewed 56 times

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 be FALSE and will come out of loop, resulting in only one number. What defines how many times this while should perform? For with this condition it will possibly generate loop infinity when it works.

  • Create another method or remove end() in getLastCode

  • OK, I’ll do it thank you very much

  • tested but did not work

  • 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 property codes will only have 1 number.

  • I understand thank you very much

Show 1 more comment

2 answers

3

An array with the 60 results:

class CodeGen{
    private $codes = array();
    public function __construct($codes) {
        $this->codes = $codes;
}
    public function getRandomCode($min, $max){  
       $next = 60; 
            while (count($this->codes) < $next) {
                $code = mt_rand($min, $max);    
                if (!in_array($code, $this->codes)) {           
                    $this->codes[] = $code;   
         }      
    }   
}   
    public function getLastCode(){
        return ($this->codes);   
    }
}
    $codes = array();
    $CodeGen = new CodeGen($codes);
    $CodeGen->getRandomCode(0, 60);
    print_r($CodeGen->getLastCode());

Upshot:

Array ( [0] => 11 [1] => 13 [2] => 48 [3] => 52 [4] => 51 [5] => 54 [6] => 33 [7] => 32 [8] => 38 [9] => 21 [10] => 1 [11] => 18 [12] => 58 [13] => 12 [14] => 2 [15] => 5 [16] => 28 [17] => 50 [18] => 57 [19] => 35 [20] => 7 [21] => 45 [22] => 39 [23] => 43 [24] => 26 [25] => 42 [26] => 29 [27] => 0 [28] => 34 [29] => 20 [30] => 40 [31] => 31 [32] => 46 [33] => 14 [34] => 10 [35] => 19 [36] => 60 [37] => 49 [38] => 23 [39] => 16 [40] => 36 [41] => 47 [42] => 8 [43] => 24 [44] => 22 [45] => 27 [46] => 53 [47] => 44 [48] => 30 [49] => 6 [50] => 17 [51] => 56 [52] => 25 [53] => 55 [54] => 15 [55] => 4 [56] => 3 [57] => 9 [58] => 59 [59] => 41 )
  • 1

    thank you very much, it worked perfectly

  • Samuel could help me in another situation, this array is in a folder in the models, how do I get it back to contollers and so go back to views where this a table for each random number?

0

You will need to create a method get that will return $this->codes

public function getCodes()
{
    return $this->codes;
}
  • I get it, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.