Random integers | SoloLearn: Learn to code for FREE!


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.

8/22/2018 12:24:57 AM

Ralph Darryl

7 Answers

New Answer


Please show your attempts.


Hatsy Rei I think he's challenging us.


Assignment niyo yan no? Ralph Darryl


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.


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.


yeah you are right that's my mistake


Randon rand = new Random(); rand.nextInt(50);