Java Array of Random Numbers

Java Array of Random Numbers | In order to generate random integer elements, we use the nextInt() method available in the Java Random class. To use this method we need to import the Random class as import java.util.Random then creates an object and then calls it with nextInt() so it generates some random numbers.

Java Array of Random Numbers Code

import java.util.Arrays;
import java.util.Random;

public class Main {
   public static void main(String[] args) {
      Random rand = new Random();
      int[] array = new int[5];
      for (int i = 0; i < array.length; i++) {
         array[i] = rand.nextInt();
      }
      System.out.println("Array = " + Arrays.toString(array));
   }
}

Output:

Array = [-1364371691, 2096926121, -1047932732, -84896728, 641038100]

Explanation of the above program goes as follows:-

Step-1:- Import the random class as import java.util.Random();
Step-2:- In the main class and main method create an object of the Random class, create an array with the size 5 then in the for loop use its object to cal nextInt() method to generate the number, store the number to the array.
Step-3:- The elements are some random numbers this output may vary from compiler to compiler and also it gives different output every time you run.

Generate Array Of Random Numbers Java

The Random class can be used with other data types also. Observe the below program for the demonstration. The below code prints the random number of types int, double, float, and long.

import java.util.Random;

public class Main {
   public static void main(String args[]) {
      Random rand = new Random();
      System.out.println("Random int: " + rand.nextInt());
      System.out.println("Random int from 0 to 49: " + rand.nextInt(50));
      System.out.println("Random double: " + rand.nextDouble());
      System.out.println("Random float: " + rand.nextFloat());
      System.out.println("Random long: " + rand.nextLong());
   }
}

Output:-

Random int: -1083591452
Random int from 0 to 49: 31
Random double: 0.9850339665544727
Random float: 0.41910708
Random long: -3691623217039547934

Based on the above code we can generate an array of random numbers within some range. For example:- if we want to generate an array of random numbers from 0 to 99 then we have to pass 100 in the nextInt() method.

How to Generate an Array of Random Numbers in Java having elements between 0 to 100

import java.util.Arrays;
import java.util.Random;

public class Main {
   public static void main(String[] args) {
      Random rand = new Random();
      int[] array = new int[5];
      for (int i = 0; i < array.length; i++) {
         array[i] = rand.nextInt(100);
      }
      System.out.println("Array = " + Arrays.toString(array));
   }
}

Output:-

Array = [47, 3, 82, 65, 65]

Array = [97, 21, 20, 14, 16]

Array = [70, 95, 17, 8, 99]

Generate Array of Random Numbers Java – Floating Points between 0 to 100

Let us see how to make an array of random numbers in Java of floating point numbers between 0 to 100 with 2 decimal points.

import java.util.Arrays;
import java.util.Random;

public class Main {
   public static void main(String[] args) {
      Random random = new Random();
      Double[] array = new Double[5];

      for (int i = 0; i < array.length; i++) {
         // generate a random floating point
         // between 0 to 100
         double number = random.nextDouble() * 100;

         // store with only 2 decimal points
         array[i] = Math.round(number * 100.0) / 100.0;
      }
      System.out.println("Array = " + Arrays.toString(array));
   }
}

Output:-

Array = [45.94, 0.64, 39.72, 29.08, 77.8]

Array = [11.78, 81.36, 57.21, 33.49, 92.32]

Array = [4.1, 56.58, 14.16, 27.46, 56.97]

The nextDouble() method generate floating-point value between 0 to 1. If we multiply it by 100 then we will get a floating-point number between 0 to 100.

Now, the floating-point number contains many values in decimal places and we want to round them to only two decimal places. Hence we called Math.round() method by multiplying the input value by 100.0 and later we divided 100.0 from the result to get the floating-point number only up to 2 decimal places.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *