I want to generate a 4x4 matrix that has elements that ranges from 1 to 8. I know that 4x4 matrix should contain 16 elements, that's why I want to make the range (1-8) appear twice in the matrix, making the all the elements appear twice using random function.
But when I use the random function, I don't know how to control the number of occurrences of a number. Creating a matrix using random function makes the number appear only once or more than twice.
Please help me to achieve my desired output. Here's my code so far:
int[][] mat = new int[4][4];
Random r = new Random();
for(int i = 0; i < 4; i ){
for(int j = 0; j < 4; j ){
mat[i][j] = r.nextInt(8);
}
}
for(int i=0; i<4; i ){
System.out.println(Arrays.toString(mat[i]));
}
Everytime I create a matrix I want the elements to be from 1 to 8 and the numbers appears twice like this, but randomly. Like everytime I run the code, the pattern should change.
Expected output:
[8,7,4,6]
[5,4,1,3]
[8,1,2,2]
[5,3,6,7]
CodePudding user response:
You can shuffle an array consisting of 1-8 elements twice to get a 4x4 matrix. The randomizeArray function makes use of random() function to randomly shuffle the array everytime.
import java.util.*;
class test{
public static void main(String args[]){
int[][] mat = new int[4][4];
int[] data = {1,2,3,4,5,6,7,8};
data = randomizeArray(data);
// randomize array the first time for rows 1 and 2 of the 4x4 matrix
for(int i = 0 ; i < 4; i ){
if(i==2){data=randomizeArray(data);};
// randomize array the second time for rows 3 and 4 of the 4x4 matrix
for(int j = 0; j < 4; j )
{
mat[i][j] = data[(i%2)*4 j];
}
}
for(int i=0; i<4; i ){
System.out.println(Arrays.toString(mat[i]));
}
}
public static int[] randomizeArray(int[] data) {
Random r = new Random();
for(int i = 0; i < data.length; i ){
int randomIndexSwap = r.nextInt(data.length);
int temp = data[randomIndexSwap];
data[randomIndexSwap] = data[i];
data[i] = temp;
}
return data;
}
}
CodePudding user response:
Here is one way you could create a random matrix with all elements appearing twice
import java.util.Random;
public class MatrixGenerator {
public static void main(String[] args) {
// Set the size of the matrix
int rows = 5;
int cols = 5;
// Create a random number generator
Random rand = new Random();
// Create the matrix
int[][] matrix = new int[rows][cols];
// Fill the matrix with random numbers
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
// Generate a random number and store it in the matrix
int randomNumber = rand.nextInt();
matrix[i][j] = randomNumber;
// Store the same random number in another location in the matrix
// This will cause all elements to appear twice in the matrix
matrix[j][i] = randomNumber;
}
}
// Print the matrix to the console
for (int i = 0; i < rows; i ) {
for (int j = 0; j < cols; j ) {
System.out.print(matrix[i][j] " ");
}
System.out.println();
}
}
}
In this code, we first set the number of rows and columns in the matrix. Then we create a Random object that we will use to generate random numbers.
Next, we create the matrix and fill it with random numbers using a nested loop. For each element in the matrix, we generate a random number and store it in the element's location. We then store the same random number in the element's corresponding location on the other side of the diagonal. This causes all elements to appear twice in the matrix.