samedi 11 mars 2017

Fill a 2D array with random numbers but making sure no number is repeated in a row

I am trying to fill a 2D array with random numbers from 1 to max but in such a way that no row contains more than one value and that each row has its values ordered from lowest to highest. For example:

1 3 5 6

2 4 6 7

2 2 5 9

The first two rows would be fine, but the third row contains repeated values. How do ensure this wont happen? Any help would be much appreciated :). What i have come up with so far is:

int[][] array = new int[s][c];
    for (int rows = 0; rows < array.length; rows++) {

        for (int cols = 0; cols < array[rows].length; cols++) {

            array[rows][cols] = (rand.nextInt(max)+1);
            System.out.print(array[rows][cols]);
        }
        System.out.println();




Aucun commentaire:

Enregistrer un commentaire