URGENTE - Números randômicos

9 respostas
Rooney

Como eu faço pra q nesse código os numeros não se repitam, por favor ajuda.

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

9 Respostas

pedroroxd

Cara…
Faz 1 favor pro guj:
NÃO DUPLIQUE TÓPICOS !

pedroroxd

o que você não quer que repita?
o x? o y?
o a? o b? o c? o d?
Explica direito..

a = 0 + (int)(Math.random() * 21);  
      b = 0 + (int)(Math.random() * 21);  
  
while (a==b) {
b = 0 + (int)(Math.random() * 21);  
}

      c = 0 + (int)(Math.random() * 21);  

while (c==a || c==b) {
c = 0 + (int)(Math.random() * 21);  
}

      d = 0 + (int)(Math.random() * 21);  

while (d==a ||d==b || d==c) {
d = 0 + (int)(Math.random() * 21);  
}
Rooney

a, b, c, d

Rooney

da forma que vc falou o a, b, c e o d ainda se repetem, acabei de testar

CrOnNoS
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:
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

} // class
Rooney

a questão é não tem como mudar meu código pra ele funcionar?? se num tiver falem.
:?

renamed
public static void main(String[] args){
		final int MAXIMO = 20; //Intervalo de números possíveis será de 0 a MÁXIMO
		int a, b, c, d;
		
		List<Integer> lista = new ArrayList<Integer>(MAXIMO);
		
		for(int i = 0; i < MAXIMO; i++){
			lista.add(i);
		}
		
		Collections.shuffle(lista);
		
		a = lista.get(0);
		b = lista.get(1);
		c = lista.get(2);
		d = lista.get(3);
		
		System.out.println("a = " + a);
		System.out.println("b = " + b);
		System.out.println("c = " + c);
		System.out.println("d = " + d);
		
	}

ou

public static void main(String[] args){
		final int MAXIMO = 20; //Intervalo de números possíveis será de 0 a MÁXIMO
		int a, b, c, d;
		
		Set<Integer> numeros = new LinkedHashSet<Integer>(MAXIMO);
		while(numeros.size() != 4){
			numeros.add((new Random()).nextInt(MAXIMO));
		}
		
		Iterator<Integer> i = numeros.iterator();
		a = i.next();
		b = i.next();
		c = i.next();
		d = i.next();
		
		System.out.println("a = " + a);
		System.out.println("b = " + b);
		System.out.println("c = " + c);
		System.out.println("d = " + d);
		
		
	}

ok?

CrOnNoS

Rooney:
a questão é não tem como mudar meu código pra ele funcionar?? se num tiver falem.
:?

… … … pois é, o que eu propus foi um método que você poderia usar no seu código. Basta mandar o método gerar os números no lugar dos Math.random

Luca

Olá

URGENTE - Números randômicos?

Tentou 193?

[]s
Luca

Criado 14 de dezembro de 2009
Ultima resposta 15 de dez. de 2009
Respostas 9
Participantes 5