Nested for loop and random. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 ?

14th Dec 2019, 11:52 AM
mstyldz
2 Answers
+ 3
Inside loop get random number using random method and store that number in Array.
14th Dec 2019, 12:07 PM
A͢J
A͢J - avatar
+ 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.
14th Dec 2019, 12:17 PM
Denise Roßberg
Denise Roßberg - avatar