VOOZH about

URL: https://www.geeksforgeeks.org/java/add-random-number-to-an-array-in-java/

⇱ How to Add Random Number to an Array in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Add Random Number to an Array in Java?

Last Updated : 23 Jul, 2025

To generate an array of integers with random values the nextInt() method from the java.util.Randomclass is used. From the random number generator sequence, this method returns the next random integer value.

Assigning a Random Value to an Array

We can assign random values to an array by two approaches. If we do not pass any arguments to the nextInt() method, it will assign some arbitrary random value of the integer range in Java. If we pass some argument to the nextInt() method, it will generate numbers in the range between 0 to the argument passed.

Example 1: In this, we assign random values to an array without any range or between the minimum value of integers to the maximum value of integers (i.e. not passing arguments).


Output
[-500572953, 583909042, -501070326, 290658844, 1861542031]

Example 2: In this, we are assigning random values to array with some specific range (i.e. passing arguments)


Output
[19, 68, 84, 53, 26]
Comment