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.
/!\ 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.
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...
– Guilherme Cipriano
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.
– Diego Schmidt