Fill Array With Random Numbers in Java

The arrays are a commonly used data structure that stores the data in contiguous memory. In this article, you will understand different methods to fill the array with random numbers in Java.

We will generate the random numbers using different library methods such as the Java Utility library’s Random class, the Java’s Math class, and the Apache Commons library.

Fill the Array With Random Numbers in Java With the Random Class

The Random class in Java’s Utility library provides a bunch of methods that can generate random numbers.

Different methods can generate random numbers with different data types. If you want to generate random numbers within a range, you can use different polymorphic forms.

However, in this article, we will fill an integer array with the numbers within the range of the integer data type. Therefore, we will not put any restrictions on the random numbers.

Fill the Array With Random Numbers Using the nextInt() Method

The nextInt() method in the Random class returns a pseudo-random integer number within the range of the int data type. The numbers are generated with a nearly uniform distribution.

The nextInt() method is defined in the Random class as given below.

To fill the array with random numbers with the nextInt() method,

  • Declare and instantiate an integer array of any size.
  • Declare and instantiate an object of the Random class.
  • Iterate the array and invoke the nextInt() method.
    • Assign the value returned to the current array position.

Let us see the code.

Output:

Randomly filled array elements are:
384255672 -174688078 483929535 731995201 254320427 1094589344

Note that there are very large numbers as well as negative numbers in the array. If you want to produce numbers within a range you can check other polymorphic forms of the nextInt() method and other methods here.

Fill the Array With Random Numbers Using the ints() Method

Unlike the nextInt() method that produces a single number at a time, the ints() method produces a stream of pseudo-random numbers. The numbers are uniformly distributed.

The ints() method is defined in the random class as given below.

Note that the method returns an IntStream object rather than an integer.

You can fill the array using this method by following the given steps.

  • Declare and instantiate an integer array of any size.
  • Declare and instantiate an object of the Random class.
  • Invoke the ints() method using the Random class instance, it will return an IntStream class object.
    • Invoke the limit() method of the IntStream class to extract the numbers equal to the size of the array from the infinite stream.
    • Invoke the toArray() method to convert the stream to the array.

Let us see the code.

Output:

Randomly filled array elements are:
2062421650 -2049005340 -1399535828 -425229654 190823518 55983318

You can check the other polymorphs of the ints() method here.

Fill the Array With Random Numbers in Java Using the Apache Commons Library

The Apache Commons library is a third-party open-source Java library. It has the RandomUtils class that you can use to produce pseudo-random numbers.
You need to add below dependency to get Apache commons jar in pom.xml.

Different methods can be used to generate numbers of different data types. You can use the nextInt() method to produce the random integer numbers.

The definition of the nextInt() method of Apache Commons library is given below.

The method returns a positive integer number including zero within the range of int data type.

This method works similar to the Random class’ nextInt() method except that you do not need to instantiate the class object. It is a static method so it can be called using the class name.

Let us see the code.

Output:

Randomly filled array elements are:
1653009679 1873237419 1198525296 1224407261 1608351756 612826226

For other polymorphic forms of the nextInt() method that can produce numbers within a given range and other methods to produce random numbers, you can visit this page.

Fill the Array With Random Numbers in Java Using the Java Math Class

The Java Math class has a random() method that returns a pseudo-random double number between 0.0 (inclusive) to 1.0 (exclusive).

The random method is defined in the Math class as given below.

You can use the random() method to populate your array with random numbers. However, if you want integer numbers, you should cast the numbers explicitly to the integer data type.

Before casting make sure that you multiply the number with a large integer so that you can lower the probability of getting the repetitive numbers.

You can fill the integer array with random numbers using the Math class by following the given steps.

  • Declare and instantiate an integer array.
  • Iterate the array,
    • Generate a double number by invoking the Math.random() method.
    • Multiply the result with a large integer. The code given here multiplies the number with 1e6.
    • Cast the result to int data type and store it into the array.

Let us see the code.

Output:

Randomly filled array elements are:
797056 217515 694273 810260 308203 952512

Fill the Array With Random Numbers in Java in a Concurrent Environment

Java provides a special class to produce random numbers in the concurrent environment.

The ThreadLocalRandom class is a subclass of the Random class and can be performance effective in a concurrent environment.

The nextInt() method of this class is similar to the nextInt() method of the Random class. You can take the following steps to fill the array with random numbers.

  • Declare and instantiate the array.
  • Iterate the array,
    • Get the instance of the class for the current thread using the static current() method.
    • Invoke the nextInt() method and assign the number to the current array element.

Let us see the code.

Output:

Randomly filled array elements are:
1069164748 -622761547 1121608439 73889954 2082012432 493374267

You can read about more methods of this class here.

Conclusion

This article has shown you three different methods to fill the array with random numbers in Java.

Other methods are thread-safe but they can have degraded performance in a concurrent environment. Therefore, you should use the ThreadLocalRandom class in a concurrent environment.

Although all methods are useful, using ints() is simplest as it does the job in a single line of code.

If you need to generate numbers within a given range, you can read our other article here.

Hope you have enjoyed the article. Stay tuned for more articles. Happy Learning!

Was this post helpful?

Leave a Reply

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