webarto

Untitled

Dec 12th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. <?php
  2.  
  3. class Lotto
  4. {
  5.   protected $_drum = [];
  6.   protected $_count = 8;
  7.   protected $_min = 1;
  8.   protected $_max = 42;
  9.  
  10.   public function __construct($count = NULL, $min = NULL, $max = NULL)
  11.   {
  12.     if($count)
  13.       $this->_count = $count;
  14.     if($min)
  15.       $this->_min = $min;
  16.     if($max)
  17.       $this->_max = $max;
  18.        
  19.   }
  20.  
  21.   public function generateSet()
  22.   {
  23.     if(count($this->_drum) === $this->_count)
  24.       $this->_drum = [];
  25.  
  26.     do
  27.     {
  28.       $number = $this->generateNumber();
  29.       if( ! in_array($number, $this->_drum))
  30.         $this->_drum[] = $number;
  31.     }
  32.     while(count($this->_drum) < $this->_count);
  33.    
  34.     return true;
  35.   }
  36.  
  37.   private function generateNumber()
  38.   {
  39.     return mt_rand($this->_min, $this->_max);
  40.   }
  41.  
  42.   public function getSet()
  43.   {
  44.     return $this->_drum;
  45.   }
  46.  
  47.   public function __toString()
  48.   {
  49.     return implode(' ', $this->_drum);
  50.   }
  51.  
  52. }
  53.  
  54. $lotto = new Lotto;
  55. for($i = 0; $i < 5; $i++)
  56. {
  57.   if($lotto->generateSet())
  58.     echo $lotto, PHP_EOL;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment