Doubt about how to generate random numbers

Asked

Viewed 327 times

0

I’m doing an exercise in java. I managed to generate 6 random numbers, now sometimes the number is repeating. Someone could help me?

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

}
  • You are asking for a random number with upper limit of 60+1, that’s right?

  • Add the random number in an array, but before adding the number in the array check that the number no longer appears in the array, then display the final contents of the array

4 answers

1

In Java 8 there is the method ints() to generate an array of integer numbers:

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

Running on IDEONE.

0

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

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

the method contemNumeroNoArray gets a número and a array and verify that this number exists in the array past tense.

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

}
  • Thanks for the help, I had done much like that shape, my teacher asked me not to use Collection.

  • Ready @daysonrodrigues, if you can’t use Arraylist, is there with normal array.

  • Thank you so much Thank you so much!

  • @daysonrodrigues if my answer solved your problem. accept it as an answer.

0

It’s a simpler alternative. You can generate a random number from a system time (System.nanoTime())... Checks if it is repeated and puts it in an array and then prints that 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));
    }
}

0

You can create an integer Arraylist and fill in the values of the ranges. Then you can use the Random class to draw an Arraylit Dice and then remove this Arraylist Dice, so the numbers will not change.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.