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?

Related Posts

  • Java random number between 1 and 100
    07 March

    Generate Random Number Between 1 and 100 in Java

    Table of ContentsUsing Java Util’s Random Class to Generate Random Number Between 1 and 100 in JavaThe nextint() MethodThe ints() MethodUsing Math Class to to Generate Random Number Between 1 and 100 in JavaUsing Apache Commons Library to Generate Random Number Between 1 and 100 in JavaUsing ThreadLocalRandom Library to Generate Random Number Between 1 […]

  • 05 October

    Get random number between 0 and 1 in java

    In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. We can simply use Math.random() method to get random number between 0 to 1. Math.random method returns double value between o(inclusive) to 1(exclusive). [crayon-6307f9bb53bfb099963385/] When you run above program, […]

  • 30 March

    java random number between 1 and 10

    Table of ContentsUsing random.nextInt() to generate random number between 1 and 10 Using Math.random() to generate random number between 1 and 10 Using ThreadLocalRandom.current.nextInt() to generate random number between 1 and 10 Was this post helpful? We have already seen random number generator in java.In this post, we will address specific query on how to […]

  • 13 December

    Java Random nextDouble

    Table of ContentsSyntaxReturnExampleGenerate double in the rangeWas this post helpful? In this tutorial, we will see Java Random nextDouble method.It is used to generate random double. It returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence. Syntax [crayon-6307f9bb54888028146152/] Here random is object of the java.util.Random class. […]

  • 13 December

    Java Random nextInt

    Table of ContentsnextInt()SyntaxReturnExamplenextInt(int bound)SyntaxReturnExampleWas this post helpful? In this tutorial, we will see Java Random nextInt method.It is used to generate random integer. There are two overloaded versions for Random nextInt method. nextInt() Syntax [crayon-6307f9bb54eea926971478/] Here random is object of the java.util.Random class. Return returns random integer. Example Let’s see a very simple example: [crayon-6307f9bb54ef0015576248/] […]

  • 13 December

    Java – generate random String

    Table of ContentsUsing simple java code with RandomUsing Apache Common langCreate AlphaNumericStringCreate random Alphabetic StringWas this post helpful? In this tutorial, we will see how to generate random String in java. There are many ways to generate random String.Let’s explore some of ways to generate random String. Using simple java code with Random You can […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.