Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Lotto
- {
- protected $_drum = [];
- protected $_count = 8;
- protected $_min = 1;
- protected $_max = 42;
- public function __construct($count = NULL, $min = NULL, $max = NULL)
- {
- if($count)
- $this->_count = $count;
- if($min)
- $this->_min = $min;
- if($max)
- $this->_max = $max;
- }
- public function generateSet()
- {
- if(count($this->_drum) === $this->_count)
- $this->_drum = [];
- do
- {
- $number = $this->generateNumber();
- if( ! in_array($number, $this->_drum))
- $this->_drum[] = $number;
- }
- while(count($this->_drum) < $this->_count);
- return true;
- }
- private function generateNumber()
- {
- return mt_rand($this->_min, $this->_max);
- }
- public function getSet()
- {
- return $this->_drum;
- }
- public function __toString()
- {
- return implode(' ', $this->_drum);
- }
- }
- $lotto = new Lotto;
- for($i = 0; $i < 5; $i++)
- {
- if($lotto->generateSet())
- echo $lotto, PHP_EOL;
- }
Advertisement
Add Comment
Please, Sign In to add comment