Java, random numbers (no repetition)

Asked

Viewed 4,816 times

3

I have a doubt in generating numbers without repetition.

I can already generate random numbers from 1 to 8 which is my goal. The catch is that it generates repeated numbers (duplicated, triplicate or even more). In my program there can be no repeated number. My code so far:

for (int k = 1; k < (gameBtn.length / 2) + 1; k++)    // 1 até 8
        {
            int z= 8 + (int)(Math.random() * (1 - 8));   // Máximo 8, mínimo 1
            gameList.add(z);    // Imprime os números de 1 a 8, podendo sair repetidos
        } 

Which means I want to make one if (numeros_anteriores_que_sairam != novo numero). If you confirm, print gameList.add(z), otherwise it doesn’t print the number that came out, (or it eliminates equal numbers) or does anything else without printing the number. Every number has to be different!

  • (gameBtn.length / 2) + 1 is 8?

  • gameBtn.lenght is 16 , so 16/2=8, if sum one gets 9,

  • did k=1 up to 9, but could have done k=0 up to 8

  • Do you want from 1 to 8, from 0 to 8 or from 1 to 9? Anyway, I think my answer serves, if only to add a 0 or a 9 to it.

2 answers

5


Try this:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Aleatorio8 {
    public static void main(String[] args) {
        List<Integer> lista = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        Collections.shuffle(lista);
        System.out.println(lista);
    }
}

In your code I think it would look like this:

gameList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
Collections.shuffle(gameList);

Or maybe so:

List<Integer> lista = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
Collections.shuffle(lista);
gameList.addAll(lista);
  • It’s working as I wanted it without repetition! , now I just have to implement it in my code above.

  • @insyspower If the answer serves you, be sure to mark it as accepted. Otherwise, we will talk to you to solve your problem.

  • Thank you very much, you solved everything perfectly, Reply accepted. I have a new doubt, I have a button (Jbutton) name "shuffle" that is already implemented, I wanted to do the following: when triggered this button, it shuffles the numbers as described above. No need to restart the application.

  • @insyspower Post a new question with that from there, including the code you use on the button.

0

This is method pattern to generate random numbers in Java:

import java.util.Random;

/**
 * Returns a pseudo-random number between min and max, inclusive.
 * The difference between min and max can be at most
 * <code>Integer.MAX_VALUE - 1</code>.
 *
 * @param min Minimum value
 * @param max Maximum value.  Must be greater than min.
 * @return Integer between min and max, inclusive.
 * @see java.util.Random#nextInt(int)
 */
public static int randInt(int min, int max) {


    Random rand = new Random();

    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

Behold the Relevant Javadoc. In practice, the class java.util. is often replaced by java.lang.Math.Random().

Browser other questions tagged

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