public static int[][] Matrix(int n, int max, int min) {
int[][] grid = new int[3][3];
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
int value = Math.abs((min + rand.nextInt((max - min) + 1)));
grid[i][j] = value;
grid[j][i] = value;
}
}
return grid;
}
The following code prints a 2D symmetric array where the values are random numbers between a range (min and max) which prints the following result as example:
0 14 11
14 0 17
11 17 0
My problem with the code is it only prints 0 as the diagonal value. How can I change it to print the diagonal values where they are set as int min instead of 0? For example, in the code above int min is 8 hence it would give this result:
8 14 11
14 8 17
11 17 8