Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Lottery
- {
- private $start;
- private $end;
- public $tempBalls = [];
- public function __construct($start = 1, $end = 49)
- {
- $this->start = $start;
- $this->end = $end;
- }
- public function balls($num = 5)
- {
- $balls = range($this->start, $this->end);
- $results = [];
- do {
- $results[] = $this->randBall($balls);
- } while (count($results) < $num);
- $result = implode(', ', $results);
- $this->tempBalls = [];
- return $result;
- }
- private function randBall($range)
- {
- $ball = $range[mt_rand(0, count($range) - 1)];
- if (!in_array($ball, $this->tempBalls)) {
- $this->tempBalls[] = $ball;
- } else {
- return $this->randBall($range);
- }
- return $ball;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement