0

# Nested for loop and random.

I try to create 0-100 random number , but it include array and random for loops , i mean that ( for( i=0;i<input1;i++) and ( j=0;j<input2;j++) under these two , system guess randomly number with array form!!! How can i do that ? Any one want to help me ?

2 Answers

+ 3

Inside loop get random number using random method and store that number in Array.

+ 1

import java.util.Random;
Random rand = new Random();
int[][] arr = new int[input1][input2];
for(int i = 0; i < input1; i++){
for(int j = 0; j < input2; j++){
arr[i][j] = rand.nextInt(101);
}
}
rand.nextInt(101) returns a number between 0 - 100 (0 & 100 inclusive)
rand.nextInt(6) + 1 would return a number between 1 - 6
-> number between 0 - 5 + 1
Hope this helps. Happy coding.