Reputation: 5
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();
Again i would like the elements in each row to be ordered from lowest to highest and im not sure how to do this!
Upvotes: 1
Views: 1953
Reputation: 2969
you should store the generated row values in an arraylist
and then check if the new values are in the arraylist
.
int[][] array = new int[3][4];
ArrayList<Integer> row = new ArrayList<>(); //represent a row
for (int rows = 0; rows < array.length; rows++) {
for (int cols = 0; cols < array[rows].length; cols++) {
int randomNumber = rand.nextInt(max)+1; //random number
//checking if the generated value is in the row
//otherwise keep on generating new value.
while(!row.contains(randomNumber)){
randomNumber = rand.nextInt(max)+1;
}
array[rows][cols] = (randomNumber);
//end of an row we should clear the previous row.
row.clear();
}
Arrays.sort(array[rows]);
}
Upvotes: 0
Reputation: 1251
Before you add the value to the array, check if it is already there:
int prevNum = 0;
while(true) {
int num = (rand.nextInt(max) + 1);
if(!(Arrays.asList(array).contains(num)) && num > prevNum && num < max - 4) {
array[rows][cols] = num;
prevNum = num;
break;
}
}
The while
loop will keep going until num
is not already in the array, it is greater than the previous number, and it is at least 4 less than max
(becuase that's how many values are in the second arrays, and we don't want a max on the first try), when it will break
(terminate the while
loop).
Upvotes: 2
Reputation: 1099
Your best bet is to split this problem up. generate sets of unique random numbers, then sort each row after it's generated.
To generate a set of unique random numbers you can go either of two ways, either keep generating numbers and check each one against the already-generated ones, or create a set of values that you're drawing from at random, and remove values from that set as you go. which one would be easiest would depend on how many possible values you are drawing from.
e.g. you could make an array:
int[] choices = {0,1,2,3,4,5,6,7,8,9};
Then take a random roll from 0-9, giving you your number. Whichever one you take, swap the 9 into that slot. so e.g. if you rolled "4", the array would become
int[] choices = {0,1,2,3,9,5,6,7,8,-};
Then you roll 0-8, and swap in the 8 position with whatever is removed. Repeat this for as many numbers as needed. Obviously this will only work for cases where the number of choices is fairly small. If the number of choices is large (e.g. 32 bit numbers) then just roll at random and check against all previously created numbers.
As each number is generated, do a sorted insert into the row.
Upvotes: 0