Como eu faço pra q nesse código os numeros não se repitam, por favor ajuda.
[code]public void gerarNumero(){
/**aki vai ser gerado os numeros principais
*veja q esta sendo setado o nome em cada Label*/
int x = 0 + (int)(Math.random() * 11);
jLabel14.setText(""+x);
int y = 0 + (int)(Math.random() * 11);
jLabel15.setText(""+y);
//gera umas das quatro alternativas para ser comparadas no if
int letra = 1 + (int)(Math.random()*4);
//aki ele somara os dois numeros gerados
//Resposta correta
setResposta(x + y);
//respostas aleatorias
a = 0 + (int)(Math.random() * 21);
b = 0 + (int)(Math.random() * 21);
c = 0 + (int)(Math.random() * 21);
d = 0 + (int)(Math.random() * 21);[/code]
Quase certeza que já vi incontáveis respostas para essa questão e se ainda não adiantou … well …
Uma das possíveis soluções:
[code]public class RandomNumbers {
private List<Integer> numerosGerados = new ArrayList<Integer>();
private Random gerador = new Random();
private int range;
public RandomNumbers(int range) {
this.range = range;
}
public int gerarNumeroAleatorio() {
int retorno;
retorno = gerador.nextInt(range);
if (numerosGerados.contains(retorno))
return -1;
else {
numerosGerados.add(retorno);
return retorno;
}
} // gerarNumeroAleatorio
public static void main(String[] args) {
RandomNumbers gerador = new RandomNumbers(10);
int numeroGerado;
for (int i = 0; i < 100; i++) {
numeroGerado = gerador.gerarNumeroAleatorio();
if (numeroGerado != -1)
System.out.println(numeroGerado);
} // for
} // main