Question

I am encountering a problem generating 6 random numbers between 1 and 6 in Java. All the numbers have to be unique. When I enter kolon value 5, the arrays should be like this:

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

I don't want the program to generate the same two numbers. What is wrong here?

Relevant code:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter row quantity: ");

    int kolon = input.nextInt();

    Integer[][] dizi_asil = new Integer[kolon][6];

    for (int i = 0; i < kolon; i++) {
        Integer[] dizi = new Integer[6];

        for (int j = 0; j < 6; j++) { 

            dizi[j] = (int) ((Math.random() * 6) + 1);  

            for (int u = 0; u < 1; u++) { 

                for (int k = 0; k < j; k++) { 

                    while (dizi[k] == dizi[j]) { 
                        dizi[j] = (int) ((Math.random()* 6)  + 1);
                        u++;
                    }

                }
            }
            dizi_asil[i][j] = dizi[j];
        }
        Arrays.sort(dizi_asil[i]);
    }

    for (int i = 0; i < dizi_asil.length; i++) {
        for (int k = 0; k < dizi_asil[i].length; k++) {
            System.out.print(dizi_asil[i][k] + "\t");
        }
        System.out.println();
    }
Was it helpful?

Solution 3

A very simple fix - replace u++; with u--;. ++ will make the loop stop, -- will make it carry on.

Though I'd suggest something more like the below. I hope it's easy enough to understand.

Integer[] dizi = new Integer[6];

for (int j = 0; j < 6; j++)
{
  boolean isValid;
  do
  {
     dizi[j] = (int) ((Math.random() * 6) + 1);
     isValid = true;
     for (int k = 0; isValid && k < j; k++)
        if (dizi[k] == dizi[j])
           isValid = false;
  }
  while (!isValid);
  dizi_asil[i][j] = dizi[j];
}

I'd also suggest the Random class, which has a nextInt(int) method, which is better than (int) ((Math.random() * 6) + 1).

But shuffling is probably a faster way to do it. Either use the API like one of the other answers or look into the Fisher-Yates / Knuth shuffle for an easy shuffle algorithm.

OTHER TIPS

create a list containing 1 to 6. then shuffle it using Collection.shuffle. Then you will get random unique number

This is an easy way:

List<Integer> list = Arrays.asList(1,2,3,4,5,6);
Collections.shuffle(list);
// you can convert it to an array if you need to.

Try Collections.shuffle(list);

List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i<7 ; i++)
    list.add(i);

Or you can do :

List<Integer> list = Arrays.asList(1,2,3,4,5,6)

Collections.shuffle(list);

Iterate the list and get unique value each time.

A much shorter way is to use shuffle as many have mentioned already.

public static void main(String... ignored) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter row quantity: ");

    List<Integer> rolls = Arrays.asList(1, 2, 3, 4, 5, 6);
    for (int i = 0, rows = input.nextInt(); i < rows; i++) {
        Collections.shuffle(rolls);
        System.out.println(rolls);
    }
}

if you run it you get something like

Please enter row quantity: 
5
[5, 6, 3, 2, 4, 1]
[5, 4, 3, 6, 2, 1]
[6, 4, 2, 3, 5, 1]
[3, 1, 6, 2, 5, 4]
[4, 1, 6, 3, 5, 2]

Imports:

import acm.util.RandomGenerator;
private RandomGenerator rgen = RandomGenerator.getInstance();

Actually picking:

randnum = rgen.nextInt(1, 6);

This picks a random number between 0 and 5. You can just do this for 1 through 6:

randnum = rgen.nextInt(2, 7);

Or:

randnum = rgen.nextInt(1, 6);
randnum++;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top