Random numbers repeating themselves

Asked

Viewed 96 times

1

Well guys, here’s the thing: I have a method that generates random numbers, and in Activity I try not to repeat these numbers, but it happens that they repeat themselves. What I’m doing wrong?

Method:

private int numeroAleatorio() {
    int numero = 0;
    //set aumenta a quantidade dos números gerados
    if (set == 1) {
        List<Integer> numeros = new ArrayList<Integer>();
        for (int i = 1; i < 11; i++) {
            numeros.add(i);
        }
        Collections.shuffle(numeros);
        numero = (Integer) numeros.get(0);
    }
    if (set == 2) {
        List<Integer> numeros = new ArrayList<Integer>();
        for (int i = 1; i < 21; i++) {
            numeros.add(i);
        }
        Collections.shuffle(numeros);
        numero = (Integer) numeros.get(0);
    }
    if (set == 3) {
        List<Integer> numeros = new ArrayList<Integer>();
        for (int i = 1; i < 31; i++) {
            numeros.add(i);
        }
        Collections.shuffle(numeros);
        numero = (Integer) numeros.get(0);
    }
    return numero;
}

Activity (not to repeat the number):

 esc = numeroDecisao();
 res1 = numeroAleatorio();
 res2 = numeroAleatorio();

 resposta = res1 + res2;

 if (esc == 1) {
 val1 = resposta;
 val2 = numeroAleatorio() + numeroAleatorio();
 val3 = numeroAleatorio() + numeroAleatorio();
 val4 = numeroAleatorio() + numeroAleatorio();

 do {
       if ((val2 == val1) || (val2 == val3) || (val2 == val4)) {
            val2 = numeroAleatorio() + numeroAleatorio();

      } else {
            passou = 1;
      }
  } while (passou != 1);

do {
     if ((val3 == val1) || (val3 == val2) || (val3 == val4)) {
       val3 = numeroAleatorio() + numeroAleatorio();
     } else {
    passou = 1;
     }
  } while (passou != 1);

 do {
      if ((val4 == val1) || (val4 == val2) || (val4 == val3)) {

        val4 = numeroAleatorio() + numeroAleatorio();
      } else {
        passou = 1;
       }
   } while (passou != 1);

                    } else if (esc == 2) {
                        val1 = (numeroAleatorio() + numeroAleatorio());
                        val2 = resposta;
                        val3 = (numeroAleatorio() + numeroAleatorio());
                        val4 = (numeroAleatorio() + numeroAleatorio());

                        do {
                            if ((val1 == val2) || (val1 == val3) || (val1 == val4)) {

                                val1 = numeroAleatorio() + numeroAleatorio();
                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);

                        do {
                            if ((val3 == val2) || (val3 == val1) || (val3 == val4)) {

                                val3 = numeroAleatorio() + numeroAleatorio();
                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);

                        do {
                            if ((val4 == val2) || (val4 == val1) || (val4 == val3)) {
                                val4 = numeroAleatorio() + numeroAleatorio();

                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);

                    } else if (esc == 3) {
                        val1 = (numeroAleatorio() + numeroAleatorio());
                        val2 = (numeroAleatorio() + numeroAleatorio());
                        val3 = resposta;
                        val4 = (numeroAleatorio() + numeroAleatorio());

                        do {
                            if ((val1 == val3) || (val1 == val2) || (val1 == val4)) {
                                val1 = numeroAleatorio() + numeroAleatorio();

                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);

                        do {
                            if ((val2 == val3) || (val2 == val1) || (val2 == val4)) {
                                val2 = numeroAleatorio() + numeroAleatorio();

                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);

                        do {
                            if ((val4 == val3) || (val4 == val1) || (val4 == val2)) {

                                val4 = numeroAleatorio() + numeroAleatorio();
                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);

                    } else if (esc == 4) {
                        val1 = (numeroAleatorio() + numeroAleatorio());
                        val2 = (numeroAleatorio() + numeroAleatorio());
                        val3 = (numeroAleatorio() + numeroAleatorio());
                        val4 = resposta;

                        do {
                            if ((val1 == val4) || (val1 == val2) || (val1 == val3)) {
                                val1 = numeroAleatorio() + numeroAleatorio();
                            } else {

                                passou = 1;
                            }
                        } while (passou != 1);

                        do {
                            if ((val2 == val4) || (val2 == val1) || (val2 == val3)) {

                                val2 = numeroAleatorio() + numeroAleatorio();
                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);

                        do {
                            if ((val3 == val4) || (val3 == val1) || (val3 == val2)) {

                                val3 = numeroAleatorio() + numeroAleatorio();
                            } else {
                                passou = 1;
                            }
                        } while (passou != 1);
                    }

                    num1.setText(Integer.toString(res1));
                    num2.setText(Integer.toString(res2));
                    op1.setText(Integer.toString(val1));
                    op2.setText(Integer.toString(val2));
                    op3.setText(Integer.toString(val3));
                    op4.setText(Integer.toString(val4));
  • 1

    @Victorstafusa Probably. This code is absurdly more complex than it should, which makes it easier to make mistakes.

  • I understand, but I would have a solution?

  • Christian, what are you trying to do? What you wanted or didn’t want to appear on num1, num2, op1, op2, op3 and op4?

  • The way you build sequences they don’t have repeated numbers. However, different calls to the method numeroAleatorio() may result in equal numbers.

  • num1 and num2 are random numbers part, in op1, op2, op3, op4 are results of addition accounts, which are calculated through the do - while. At the beginning there is the if (Esc == 1) that an if generates random numbers from 1 to 4, and for each case the answer between the sum of num1 + num2, is set in a button (op1, op2, op3, op4). For example if Esc == 1 is the answer of num1+num2, it will be in op1 and the others have wrong answers. Sometimes it happens that these answers stay the same.

Show 1 more comment

1 answer

3


You can try to do that:

private int numeroAleatorio() {
    return (int) (Math.random() * set * 10 + 1);
}
int esc = numeroDecisao();
int res1 = numerosAleatorio();
int res2 = numerosAleatorio();
int soma = res1 + res2;

Set<Integer> respostas = new HashSet<>();
respostas.add(soma);

while (respostas.size() < 4) {
    int a = numeroAleatorio();
    int b = numeroAleatorio();
    respostas.add(a + b);
}

respostas.remove(soma);

Iterator<Integer> it = respostas.iterator();

num1.setText(Integer.toString(res1));
num2.setText(Integer.toString(res2));
op1.setText(Integer.toString(esc == 1 ? soma : it.next()));
op2.setText(Integer.toString(esc == 2 ? soma : it.next()));
op3.setText(Integer.toString(esc == 3 ? soma : it.next()));
op4.setText(Integer.toString(esc == 4 ? soma : it.next()));

His mistakes were that:

  • Note that if the set is 1, you want to generate numbers from 1 to 10, if it is 2, whether from 1 to 20 and if it is 3, from 1 to 30. So, you want to generate numbers from 1 to set * 10. Always prefer to use math than a lot of ifs with copied and pasted code.

  • To choose one random number, you generated a lot of numbers (according to the 10 * set) and scrambled them to get only the first. There is a mathematical formula that gives this result in a simpler way:

    1. Start with a random real number x such that 0 <= x < 1 (Math.random() produces this).

    2. Multiply x by the amount of numbers existing in the desired range. As the numbers are 1 up set * 10, then the amount of numbers in the desired range is set * 10. This will produce a real number such that 0 <= x < set * 10, that is, a number of 0 up to set * 10 - 1.

    3. Add the minimum range limit, which in your case is 1. This will generate a real number in the range of 1 until set * 10.

    4. Make a cast for int, so the result will be an integer of 1 until set * 10.

    And all this above can be expressed in one line:

    return (int) (Math.random() * set * 10 + 1);
    
  • All cases separated by esc are similar, so it is best to avoid separating them. Remember the if serves to separate distinct cases, not similar cases with minor differences.

  • The Set is a set that does not allow repetitions. So it is easier to add numbers to them until you have 4 different numbers.

  • The Iterator it is possible to remove elements from a collection one by one by means of the method next().

  • The ternary operator ? : is your friend. Know how to use it (but don’t abuse it).

  • Victor, thanks I haven’t had a chance to test, but as soon as possible I’ll give my feedback

  • Victor, I had a chance to test it and it worked for a situation, it was worth it. But for a different situation did not solve the problem of repetition. The situation is as follows: I have an account on the screen type like this: x + 1 = 2, the user has to click on the option that completes the account and I tried to use this code that you passed, but it did not work, as I could change it to the situation I described earlier?

  • @Christiangomesdasilva In this case I recommend creating a new question. Your code should already be quite different and that’s why you are now putting is already a fairly different problem. Ask the new question, a link to this one.

  • Victor, how do I put the link in the question?

  • @Christiangomesdasilva [minha pergunta anterior](https://answall.com/q/254984/80918) - with no spaces in the middle.

  • I asked the new question

Show 1 more comment

Browser other questions tagged

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