I have here a 2D Array I populate with random numbers. I populate my array as follows:
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 5; j++)
{
nums[i][j] = 1 + (int)(Math.random() * 60);
}
}
I want my array to have numbers between 0 and 60, hence the:
* 60
I display my numbers like this:
System.out.println("Nums are: " + nums[0][0]+ " " + nums[0][1]+ " " + nums[0][2]+ " " + nums[0][3]+ " " + nums[0][4]);
and the output looks like this
Nums are: 43 6 32 6 12
I want to populate my array with Random numbers, but I don't want it to have matching numbers, so for this instance the number 6 is in the array twice. I want to remove the duplicates from the array or just not put them in the array at all.
The way I was thinking of doing this was by having 2 additional for loops within my loops with a greater value than 0, this way the I could have an if statement that checks if the current position in the array is equal to the next, like this:
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 5; j++)
{
nums[i][j] = 1 + (int)(Math.random() * 60);
for(int l = 1; l < 3; l++){
for(int m = 1; m < 5; m++){
if(nums[i][j] == nums[l][m]){
nums[i][j] = 1 + (int)(Math.random() * 60);
}
}
}
As you can see here I have added l and m which both have a number 1, which will put them one above the previous position. Also my if statement checks if the values are same, and if they are, it randoms the numbers again.
This is not working for me, could someone give me a hand please, what am I doing wrong ?