vendredi 25 septembre 2015

generating unique row and column in 2 dimensional array

I am randomly filling a 2 dimensional array but i want to have the numbers generated in each row and column to be unique. Here is the code i have used

int[][] values = new int[3][3];

    Random randomGenerator = new Random();
    for (int i = 0; i < values.length; i++) {

        int[] sub = values[i];
        for (int x = 0; x < sub.length; x++) {
            sub[x]= randomGenerator.nextInt(4);;
        System.out.print(sub[x] + " ");
        }
        System.out.println();
    }

My current output is and this might change as the numbers generated are random

2 2 2 
3 2 0 
0 2 1 

But i am expecting something like this

1 2 3 
3 1 2 
2 3 1 




Aucun commentaire:

Enregistrer un commentaire