Random numbers repeating

1

Well guys, it's this: I have a method that generates random numbers, and in the activity I try not to repeat those numbers, but it happens that they repeat themselves. What am I 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));
    
asked by anonymous 13.11.2017 / 17:00

1 answer

3

You can try this:

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()));

Your mistakes were that:

  • Note that if set is 1, you want to generate numbers from 1 to 10, if it's 2, or 1 to 20, and if it's 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 if s with copied and pasted code.

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

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

  • Multiply x by the number of numbers in the desired range. Since the numbers are from 1 to set * 10 , then the number 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 from 0 to set * 10 - 1 .

  • Add the minimum threshold of the range, which in your case is 1. This will generate a real number in the range of 1 to set * 10 .

  • cast for int , so the result will be an integer from 1 to set * 10 .

    And all of the above can be expressed in one line:

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

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

  • Iterator allows you to retire elements of a collection one by one using the method next() .

  • The ternary operator ? : is your friend. Learn to use it (but do not abuse it).

13.11.2017 / 17:39