Losowanie lotto

<?php

class Lotto {

    /**
     * @var int 
     */
    private $numbersQuantity;

    /**
     * @var int
     */
    private $maxNumber;

    /**
     * @param int $numbersQuantity
     * @param int $maxNumber
     */
    public function __construct(int $numbersQuantity, int $maxNumber) {
        if ($numbersQuantity <= 0 || $maxNumber <= 0) {
            throw new \Exception('Parameters musts been biggers than zero');
        }

        $this->numbersQuantity = $numbersQuantity;
        $this->maxNumber = $maxNumber;
    }

    /**
     * generate lotto numbers
     * 
     * @return array
     */
    public function generateLottoNumbers(): array {
        $numbers = range(1, $this->maxNumber);
        shuffle($numbers);

        $lottoNumbers = array_slice($numbers, - $this->numbersQuantity);

        sort($lottoNumbers);

        return $lottoNumbers;
    }

}

/**
 * @param array $data
 * @return boolean
 */
function consecutiveElementsInArray(array $data): bool {
    $count = count($data);
    foreach ($data as $key => $value) {
        if ($key == $count - 1) {
            break;
        }

        if (($data[$key + 1]) != ($value + 1)) {
            return false;
        }
    }

    return true;
}

$lottoNumbers = (new Lotto(6, 49))->generateLottoNumbers();

$interest = array_intersect($lottoNumbers, [3, 7, 9, 12, 27, 45]);

echo '<h1>trafiles: ' . count($interest) . ' liczb';
echo '<br>';

foreach ($lottoNumbers as $number) {
    echo '<b>' . $number . '</b>  ';
}

die;

$count = 0;

for ($i = 1; $i < 139; $i++) {
    $lottoNumbers = (new Lotto(6, 49))->generateLottoNumbers();
    if (consecutiveElementsInArray($lottoNumbers)) {
        $count++;
    }
}

echo '<h1>tyle ma liczby po kolei: ' . $count . '</h1>';
die;
​
Komentarze wyłączone