I’m trying to build a Megasena betting generator with the following parameters

Asked

Viewed 222 times

0

i am trying to build a mega sena betting generator with the following parameters:

number of dozens and number of games

Rules:

  1. Numbers cannot duplicate between games.
  2. Consider the dozens between 00 and 59.

Final exit:

  1 - Lista de jogos  
  2 - Lista de números que se repetiram mais de uma vez

Ex: Considering dozens quantity parameters = 7 and games = 2

Exit:

      3 - 5 - 7 - 9 - 20 - 55 - 56
      5 - 20-51 - 55 -56-  57 - 59

 Duplicados: 

    55 - 2x        
     5 - 2x

Here’s the code I’ve built so far:

<?php

function getRandomNumbers($num, $min, $max, $repeat = false, $sort = false)
{
    if ((($max - $min) + 1) >= $num) {
        $numbers = array();

        while (count($numbers) < $num) {
            $number = mt_rand($min, $max);

            if ($repeat || !in_array($number, $numbers)) {
                $numbers[] = $number;
            }
        }

        switch ($sort) {
        case SORT_ASC:
            sort($numbers);
            break;
        case SORT_DESC:
            rsort($numbers);
            break;
        }

        return $numbers;
    }

    return false;
}

?>

<?php

if ($numbers = getRandomNumbers(6, 1, 60, false, SORT_ASC)) {
    print implode(', ', $numbers);
} else {
    print 'A faixa de valores entre $min e $max deve ser igual ou superior à' .
        ' quantidade de números requisitados';
}

?>

however I cannot implement the restriction conditions, can help please?

  • The games are always 7 numbers?

2 answers

1


If you can’t repeat between them it would be easier to define which choices are possible, then:

$NumerosDiposniveis = range(0, 59);

When selecting one of them, then run:

unset($NumerosDisponiveis[$IndexQueFoiGerado]);

Basically this:

function gerarCombinacao($QntDezenas, &$NumerosDisponiveis){

    $QntDisponivel = count($NumerosDisponiveis);

    if($QntDisponivel < $QntDezenas){
        return false;
    }

    for($n = 0; $n < $QntDezenas; $n++){

        $EscolhaAleatoria = random_int(0, $QntDisponivel - ($n + 1));

        $NumerosDisponiveis = array_values($NumerosDisponiveis);

        $Combinacao[] = str_pad($NumerosDisponiveis[$EscolhaAleatoria], 2, '0', STR_PAD_LEFT);
        unset($NumerosDisponiveis[$EscolhaAleatoria]);

    }

    return implode('-', $Combinacao);

}

Then I could run:

$NumerosDisponiveis = range(0, 59);

for($i = 0; $i < 10; $i++){
    echo gerarCombinacao(6, $NumerosDisponiveis) . PHP_EOL;
}

Test this.

In case this is the maximum number that can be generated, it is not possible to generate more than 10 because you cannot repeat the numbers according to you in:

1 - Numbers cannot duplicate between games

0

As the numbers cannot be repeated between the cards another way to do this is to generate all the possible numbers (0 to 59) shuffle them and generate the maximum number of cards with seven numbers, in this case they are eight. At the end you can choose which cards will display or manipulate.

The key point of this solution is the array_chunk() which breaks the array of available numbers by size (7 elements) and thus generates the cards. array_pop() is responsible for deleting the last card since it has only 4 helmets.

Example - idoene

<?php

function gerarCartao(){
   $numeros = range(0, 59);
   shuffle($numeros);
   $cartoes = array();

   $cartoes = array_chunk($numeros, 7);
   array_pop($cartoes);
   return $cartoes;

}


print_r(gerarCartao());

The exit is something like:

Array ( [0] => Array ( [0] => 32 [1] => 55 [2] => 22 [3] => 53 [4] => 16 [5] => 38 [6] => 49 ) [1] => Array ( [0] => 44 [1] => 27 [2] => 28 [3] => 25 [4] => 47 [5] => 14 [6] => 48 )

Browser other questions tagged

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