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.

22nd Aug 2018, 12:24 AM
Ralph Darryl
Ralph Darryl - avatar
7 ответов
+ 8
Please show your attempts.
22nd Aug 2018, 1:06 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Hatsy Rei I think he's challenging us.
22nd Aug 2018, 4:40 AM
Email Not Activated
+ 2
Assignment niyo yan no? Ralph Darryl
23rd Aug 2018, 3:21 AM
Email Not Activated
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.
23rd Aug 2018, 1:49 AM
krikkitbot
krikkitbot - avatar
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.
23rd Aug 2018, 1:52 AM
krikkitbot
krikkitbot - avatar
0
yeah you are right that's my mistake
23rd Aug 2018, 2:21 AM
Rajat Tyagi
Rajat Tyagi - avatar
- 1
Randon rand = new Random(); rand.nextInt(50);
22nd Aug 2018, 1:39 AM
Rajat Tyagi
Rajat Tyagi - avatar