How do you detect three equal numbers?

Asked

Viewed 142 times

3

I have a cycle For that has two mt_rand, one of random numbers and the other mt_rand of numbers that are designated as prizes.

What I want now is to check when 3 equal numbers come out, make the sum of the values that came out in this mt_rand of the 3 numbers and return in a variable with the total sum of these values.

PHP

for ($i = 0; $i<9; $i++){
    $numeros = mt_rand(1,9);
    $premio = mt_rand(1,20);
    echo "valor" . ($i + 1) . "=$numeros&premio" . ($i + 1) . "=$premio&";
}
  • 1

    It is not clear enough this question to help you. Can you give us an example of how you want it to happen ?

  • This is a project like scratch card in the flash I have 9 squares and I have a boot buy the scratch card when I click buy it goes to php and in the cycle for generates the numbers of luck and the respective prizes that will appear in the boxes I want that when you leave 3 equal numbers it add the values of the prizes that leave in those 3 equal numeoros and return in a total variable the value to show in the flash

  • usually in other languages, we create a list, and each random number we play in the list, so it’s easy to check if the number already exists in the list in C# would look something like this: if(! listRnd[]. Contains(new))

  • Make a version of your algorithm in natural language, step by step, which I put my suggestion of the PHP version. It will be a pleasure to help!

  • A natural language version as well ?

  • 1

    As @Dorathoto said, in languages like C# and Java there are libraries that make it easy to solve this problem. You play all numbers in a list, and use, using Java as an example: Collections.Frequency(list, number) returns the frequency, List.contains(object) returns Boolean etc.. .

  • Yes but in php you can also do I think

  • The three numbers must be equal in that example set, 5,5,5 (valid), 5,5,5,8 (possible invalid). That is to each group of three, all must be equal.

Show 3 more comments

2 answers

2

The question lacks more accurate information to demonstrate a concise solution. But regardless of that, based on what posted:

$premio_soma = 0;
for ($i = 0; $i<9; $i++) {
    $n = mt_rand(1, 9);
    $premio[$n][] = mt_rand(1, 20);
    if (isset($numeros[$n])) {
        $numeros[$n]++;
        // encontrou 3 ocorrências do mesmo número
        if ($numeros[$n] == 3) {
            // teste, exibe os 3 números a somar
            echo '<pre>'; print_r($premio[$n]); echo '</pre>'; //exit;
            // some os 3 numeros
            $premio_soma = array_sum($premio[$n]);
            // interrompe o laço de repetição
            break;
        }
    } else {
        $numeros[$n] = 1;
    }
}

echo 'premio: '.$premio_soma;

It is possible to optimize and/or create a better logic. I just did it in a way following the logic of what posted based on the "structure" of the original code.

2

Another way to solve is to join each group of three and check that all are equal, note the initial value of $i went from zero to one, to identify the multiples of three:

$sorteados = array();
for($i = 1; $i<10; $i++){
    $numero = mt_rand(1,9);
    $premio = mt_rand(1,20);
    $sorteados[] = $numero;

    if($i % 3 === 0){
        if($sorteados[0] == $sorteados[1] && $sorteados[1] == $sorteados[2]){
            printf('Iguais: %s - %s - %s | prêmio: %s <br>', $sorteados[0], $sorteados[1], $sorteados[2], array_sum($sorteados));
        }else{
            printf('Sorteados: %s - %s - %s | prêmio: %s  <br>', $sorteados[0], $sorteados[1], $sorteados[2], $premio);
        }
        $sorteados = array();
    }
}

Possible exit:

Iguais: 8 - 8 - 8    | prêmio: 24
Sorteados: 8 - 1 - 1 | prêmio: 17
Sorteados: 4 - 7 - 8 | prêmio: 12 

Browser other questions tagged

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