java – Fill an array with random numbers

java – Fill an array with random numbers

You can use IntStream ints() or DoubleStream doubles() available as of java 8 in Random class. something like this will work, depends if you want double or ints etc.

Random random = new Random();

int[] array = random.ints(100000, 10,100000).toArray();

you can print the array and youll get 100000 random integers.

You need to add logic to assign random values to double[] array using randomFill method.


 public static double[] list(){
    anArray = new double[10];   
    return anArray;


 public static double[] list() {
    anArray = new double[10];
    for(int i=0;i<anArray.length;i++)
        anArray[i] = randomFill();
    return anArray;

Then you can call methods, including list() and print() in main method to generate random double values and print the double[] array in console.

 public static void main(String args[]) {


One result is as follows:


java – Fill an array with random numbers

This seems a little bit like homework. So Ill give you some hints. The good news is that youre almost there! Youve done most of the hard work already!

  • Think about a construct that can help you iterate over the array. Is there some sort of construct (a loop perhaps?) that you can use to iterate over each location in the array?
  • Within this construct, for each iteration of the loop, you will assign the value returned by randomFill() to the current location of the array.

Note: Your array is double, but you are returning ints from randomFill. So theres something you need to fix there.

Leave a Reply

Your email address will not be published.