Checking repeated numbers in PHP

Asked

Viewed 707 times

0

I’m doing a choice job that’s a lottery. In this lottery I need to check if the numbers are equal, if they are, I must substitute for another number and this number may not have already been generated. My difficulty is to be able to make a repeat loop that always checks this. Follow below the line of code:

Ps: The algorithm is to be run at the same command prompt

  • $numDezenas: Number of tens he wants to bet (Megasena,
    Lotomania, Quina and Lotofácil)

  • $apostas: Betting amount

  • $numMax: Maximum value that can be generated by the draw according to the type of bet (Megasena is 60, Quina 80, etc.)

function dezenas($numDezenas,$apostas, $numMax){

    $dezenas = [];
    $numDezenas = $numDezenas - 1;
    for ($i=0; $i < $apostas ; $i++) { //Quantidade de apostas 
        for ($j=0; $j <= $numDezenas; $j++) { //Quantidade de Dezenas
            $dezenas[$j] = rand(0, $numMax);
            for ($k=$numDezenas; $k > 0; $k--) { //Verificar se as dezenas são repetidas
                do {
                if ($dezenas[$j] == $dezenas[$k] && $j != $k) {
                    $dezenas[$j] = rand(0,$numMax);
                    } while (); //Aqui que não sei o que fazer...
                }
            }
        }
    }
}

2 answers

0


I don’t know if it’s exactly what you want, but I made a simple example.

You can see the script working on ideone.

Note: I used the str_pad to fill the values that have only one digit with 0 left to stay in the lottery pattern, follow an example to generate the numbers:

<?php
function apostar($qtdDeDezenas, $qtdDeApostas, $numMaximoDezena){
    $apostas = [];
    for ($i = 1; $i <= $qtdDeApostas; $i++) {
        $dezenas = [];
        for ($j = 1; $j <= $qtdDeDezenas; $j++) {
            while (count($dezenas) < $qtdDeDezenas) {
                $dezenaGerada = str_pad(rand(0, $numMaximoDezena), 2, 0, STR_PAD_LEFT);
                if (!in_array($dezenaGerada, $dezenas)) {
                    $dezenas[] = $dezenaGerada;
                }
            }
        }

        sort($dezenas, SORT_NUMERIC); // ordernar da dezena menor para maior
        $apostas[]['dezenas'] = $dezenas;
    }

    return $apostas;
}

$qtdDeDezenas = 6;
$qtdDeApostas = 15;
$numMaximoDezena = 60;

$apostas = apostar($qtdDeDezenas, $qtdDeApostas, $numMaximoDezena);

echo "<pre>";
print_r($apostas);
echo "<pre>";
  • 1

    Almost as good as... I noticed that the position of the arrays equals the number, I wanted it to start at 0, but the bad thing is that I can’t understand the logic of organizing the numbers without them repeating in the same bet...

  • You can use the in_array function and check if the dozen you have generated is already in the dozens array, if it is not, you put it. I edited the answer as you wish.

0

First I believe it is better to divide this into several functions, a function as the name suggests should make a single function.

However you can simply use the count() with the array_unique() and using the sort() to maintain the same order, for the sake of performance this is not ideal, however it is simple to understand the concept.

function removerDuplicidade(array $array)
{

    sort($array);
    return array_unique($array, SORT_REGULAR);

}

function criarAposta(int $quantidadeDeDezenasParaMarcar, int $numeroMaximoDisponivelNaCartela)
{

    $aposta = [];

    while (count($aposta) < $quantidadeDeDezenasParaMarcar) {

        $aposta[] = random_int(0, $numeroMaximoDisponivelNaCartela);
        $aposta = removerDuplicidade($aposta);

    }

    return $aposta;

}

The idea is simple, each number generated by random_int, requires PHP 7, will be "filtered" by removerDuplicidade, it is responsible for maintaining the ascending order (due to the sort()) and will remove the duplicities due to the array_unique().

Assuming he runs three numbers:

$aposta = [1, 10, 20];

Suppose he runs it 10, that already exists, this number will be removed by array_unique. Like the while is defined by count() the countdown will continue 3, once the fourth number has been removed.


However you need to generate more than one, so you can use a new function, very similar as a complement:

function criarMultiplasApostas(int $quantidadeDeApostas, int $quantidadeDeDezenas, int $numeroMaximoDisponivelNaCartela)
{

    $multiplasApostas = [];

    while (count($multiplasApostas) < $quantidadeDeApostas) {

        $multiplasApostas[] = criarAposta($quantidadeDeDezenas, $numeroMaximoDisponivelNaCartela);

        $multiplasApostas = removerDuplicidade($multiplasApostas);

    }

    return $multiplasApostas;

}

This will cause you to generate multiple bets, assuming you generate two:

$multiplasApostas = [
    [1,2,3,4,5,6]
    [1,2,3,4,5,6]
];

This would be removed by removerDuplicidade, because they are identical bets, for this reason the sort so that they are always in the same order and therefore removed.


Test it out here.


/!\ Flaws

One of the flaws is that it might try to do:

gerarMultiplasApostas(100, 6, 7);

This is generating 100 bets with 6 numbers each in a space of 8 numbers (0,1,2,3,4,5,6,7,8). This is mathematically impossible, the calculation of combinations is exactly:

8! / (6! * (8 - 6)!)

So there are only 28 combinations, define the 100 will create an infinite loop, which would even be possible a Dos, because each process would be locked for 30 seconds, by default.

Another flaw is that the removerDuplicidade will not work out of order, being the sort extremely necessary to generate the bets.

Browser other questions tagged

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