# Java Fill 2d Array With Random Numbers

Java Fill 2d Array With Random Numbers | In this section, we generate a random number for a two-dimensional array. To do this we need a random module that is available in java.util.Random package. Also see:- Create Java Array of Random Numbers

For example:-
array[ ][ ] = new int[2][2]
Generate random number and insert into the array.
Result:-
1 2
3 4

The random module has generated some random integers, in the program we just need to give the number of rows and columns. Observe the below code.

## Program in Java Fill 2d Array With Random Numbers

Now let us see a Java program to fill a 2d array with random numbers. We will create a 2d array.

``````import java.util.Arrays;
import java.util.Random;

class Main {
public static void main(String args[]) {
int array[][] = new int[2][2];
Random random = new Random();

for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = random.nextInt();
}
}
System.out.println("Array: "
+ Arrays.deepToString(array));
}
}``````

Output:-

Array: [ [702551432, -2129386055], [-1258240069, -1691210993] ]

The code explanation is as follows:-

Step1:- Import the required classes that are used to execute this code. We need a random class, so import random from java.util.Random; The Random class generates a random number, there are many constructors for this class.
Step2:- In the main method initialize a two-dimensional array and give the row and column size as 2 and 2 respectively.
Step3:- Create an object for the random class as random and in the for loop insert the elements of row and column by using the random method, here we use random.nextInt() as we need integer values.
Step4:- Print the result.

If we want to generate a number within a specific range then we have to pass that range max value to the nextInt() method.

``array[i][j] = random.nextInt(100);``

Output:-

Array: [ [61, 34], [30, 18] ]

Now the nextInt() method always generates numbers between 0 to 100.

## Java Fill 2d Array With Random Numbers – Floating-point values

In a similar way, we can generate random double values (floating-point values) and insert them into the 2d array. We run the same code just by replacing the random.nextInt() method to random.nextDouble().

``````import java.util.Arrays;
import java.util.Random;

class Main {
public static void main(String args[]) {
double array[][] = new double[2][2];
Random random = new Random();

for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = random.nextDouble();
}
}
System.out.println("Array: "
+ Arrays.deepToString(array));
}
}``````

Output:-

Array: [ [0.655100901068955, 0.2537286294440366], [0.861592127745446, 0.08139249678343408] ]

The nextDouble() method generates a floating-point number between 0 to 1. If we want to generate floating-point numbers in between some range then we have to multiply generated value with the max value of the range.

``array[i][j] = random.nextDouble() * 100;``

Output:-

Array: [ [74.83114169159951, 1.6052937526550237], [98.35925290059718, 62.81589398882489] ]

``````// generate a random floating point
// between 0 to 100
double number = random.nextDouble() * 100;

// store with only 2 decimal points
array[i][j] = Math.round(number * 100.0) / 100.0;``````

Output:-

Array: [ [43.38, 61.07], [95.25, 68.77] ]

## Generate Random 2d Array Java

In the above Java fill 2d array with random numbers programs, we hardcoded the number of rows and column values for a 2d array. But in the below program we will initialize them with some random number between 1 to 5.

``````import java.util.Arrays;
import java.util.Random;

class Main {
public static void main(String args[]) {
Random random = new Random();
int row = random.nextInt(5) + 1;
int column = random.nextInt(5) + 1;
int array[][] = new int[row][column];

for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = random.nextInt(100);
}
}

System.out.println("2D array size: "
+ row + " x " + column);
System.out.println("Array: "
+ Arrays.deepToString(array));
}
}``````

Output:-

2D array size: 3 x 2
Array: [ [90, 90], [47, 34], [63, 96] ]

2D array size: 2 x 4
Array: [ [88, 90, 61, 39], [65, 17, 29, 76] ]

2D array size: 1 x 1
Array: [ [76] ]

2D array size: 2 x 2
Array: [ [70, 24], [33, 72] ]

2D array size: 1 x 4
Array: [ [43, 90, 39, 50] ]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!