To get the Random int values we utilise the java.util.Math provided under java library. Since Math.random()returns random double value between 0 and 1 , we multiply it by 100 to get random numbers between 0 to 100 and then we cast it into type int.

To store random double values in an array we don't need to cast the double value returned by Math.random() function. See code below.

import java.util.Arrays;

public class ArrayRandomValues {

    public static void main(String args[]) {
        int[] myIntArray = new int[100];
        for (int i = 0; i < myIntArray.length; i++) {
            myIntArray[i] = (int) (Math.random() * 100);
        }
        System.out.println(Arrays.toString(myIntArray));

        double[] myDoubleArray = new double[100];
        for (int i = 0; i < myDoubleArray.length; i++) {
            myDoubleArray[i] = Math.random() * 100;
        }

        System.out.println(Arrays.toString(myDoubleArray));
    }

}
Comments