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));
XY problem?
– Victor Stafusa
@Victorstafusa Probably. This code is absurdly more complex than it should, which makes it easier to make mistakes.
– Maniero
I understand, but I would have a solution?
– Christian Gomes da Silva
Christian, what are you trying to do? What you wanted or didn’t want to appear on
num1
,num2
,op1
,op2
,op3
andop4
?– Victor Stafusa
The way you build sequences they don’t have repeated numbers. However, different calls to the method
numeroAleatorio()
may result in equal numbers.– ramaral
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.
– Christian Gomes da Silva