0
Random integers
Declare and initialize an array with 8 integer elements. Use the random merhod of the Math class to initialize the values with random integers between 1 and 50.
7 ответов
+ 8
Please show your attempts.
+ 2
Hatsy Rei I think he's challenging us.
+ 2
Assignment niyo yan no? Ralph Darryl
0
int[] arr = new int[8];
for (int i=0; i<8; i++) {
arr[i] = Math.random()*50 + 1;
}
Pretty sure this should also work with an enhanced for loop.
0
Rajat Tyagi: There’s a couple issues with your solution:
1. OP said to use Math.random(), not a Random object.
2. Random’s nextInt(x) method generates an integer from 0 (inclusive) to x (exclusive), so rand.nextInt(50) will actually return a number from 0-49.
0
yeah you are right that's my mistake
- 1
Randon rand = new Random();
rand.nextInt(50);