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

0

I'm trying to build a mega sena bet generator with the following parameters:

number of dozens and number of games

Rules:

  • Numbers can not be duplicated between games.
  • Consider the tens between 00 and 59.
  • Final output:

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

    Ex: Considering parameters number tens = 7 and games = 2

    Output:

          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';
    }
    
    ?>
    

    but I can not implement the restriction conditions, can you help please?

        
    asked by anonymous 30.06.2017 / 22:51

    2 answers

    1

    If you can not repeat between them it would be easier to define what the possible choices are, then:

    $NumerosDiposniveis = range(0, 59);
    

    When you select 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;
    }
    

    Try this.

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

      

    1 - Numbers can not duplicate between games

        
    30.06.2017 / 23:24
    0

    Since numbers can not be repeated between cards, another way to do this is to generate all possible numbers (0 to 59), and then the number of cards with seven numbers, in which case there are eight. At the end you can choose which cards to display or manipulate.

    The key point of this solution is array_chunk() that 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 elements.

    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 output 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 )
    
        
    30.06.2017 / 23:28