Doubt about generating random numbers

0

I'm doing an exercise in java. I was able to generate 6 random numbers, now, sometimes the number is being repeated. Could someone help me?

Follow the code:

public class MegaSenna {

    public static void main(String[] args) {

        Random radom = new Random();
        gerandoNumeroSorteio(radom);
    }

    private static void gerandoNumeroSorteio(Random radom) {
        int numeroTmp;
        for (int i = 0; i < 6; i++) {
            numeroTmp = radom.nextInt(60 + 1);
            System.out.println((i+1)+"º numero sorteado = " + numeroTmp);
        }
    }

}
    
asked by anonymous 08.11.2017 / 20:05

4 answers

1

In Java 8 there is the ints() to generate an array of integers:

// Gera um array contendo 6 números de entre 1 e 60.
int []valores = new Random().ints(6, 1, 60).toArray();

Running on IDEONE.

    
09.11.2017 / 14:59
0

Create an array for these numbers and make the method gerandoNumeroSorteio return this array.

The method was modified to create an array and to be populated always testing (with the method contemNumeroNoArray if this generated number already exists in the array.

method contemNumeroNoArray gets número and array and checks to see if that number exists in array passed.

public class MegaSenna {

    public static void main(String[] args) {

        Random radom = new Random();
        int[] randomNumbers = gerandoNumeroSorteio(radom);

        for (int i = 0; i < randomNumbers.length; i++) {
            System.out.println((i + 1) + "º numero sorteado = " + randomNumbers[i]);
        }

    }

    private static int[] gerandoNumeroSorteio(Random radom) {
        int numbers[] = new int[6];
        int numeroTmp;
        int i = 0;
        while (i < 6) {
            numeroTmp = radom.nextInt(60 + 1);
            if (!contemNumeroNoArray(numeroTmp, numbers)) {
                numbers[i] = numeroTmp;
                i++;
            }
        }

        return numbers;
    }

    private static boolean contemNumeroNoArray(int numeroTmp, int[] numbers) {
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == numeroTmp)
                return true;
        }
        return false;
    }

}
    
08.11.2017 / 20:38
0

A simpler alternative. You can generate a random number from a system time (System.nanoTime ()) ... It checks if it is repeated and placed in an array and then prints this array.

public static void main(String[] args) {

    gerandoNumeroSorteio();
}

private static void gerandoNumeroSorteio() {
    int numeroTmp;
    ArrayList<String> al = new ArrayList<String>();
    for (int i = 0; i < 6; i++) {
        Random random = new Random(System.nanoTime());
        numeroTmp = random.nextInt(60 + 1);

        if (!al.contains(numeroTmp)) {
            al.add(String.valueOf(numeroTmp));
        } else {
            System.out.println("Número repetido");
        }

        System.out.println("Nº numero sorteado = " + al.get(i));
    }
}
    
09.11.2017 / 14:17
0

You can create an Integer ArrayList and fill in the values of the ranges. Then you can use the Random class to sort an ArrayLit index and then remove this ArrayList index, so the numbers will not be repeatable.

public GeradorNumero(int min,int max) {
    r = new Random();
    this.numeros = new ArrayList<>();
    for (int i = min; i < max; i++) {
        this.numeros.add(i);
    }
}

public int gerarValor(){
    int posicao = r.nextInt(this.numeros.size());
    int valorGerado = this.numeros.get(posicao);
    this.numeros.remove(posicao);           
    return valorGerado;
}
    
04.11.2018 / 05:06