Scenario
Generation of random values is useful in many fields such as science, cryptography, statistics, simulation etc.
When related to software applications, they are particularly used to test how applications will behave in real environments.
Since user input is highly unpredictable, the applications are tested against random input values which are not supplied by humans but generated randomly by the application itself.

Java 8 streams
There are many ways to generate a random number in java. But there was no direct way of generating an array of random integers.
Java 8 introduced the concept of Streams. It also added some new methods to java.util.Random class which return a stream.
A stream can be utilized to generate an array.
Following are the methods added to java.util.Random class which return a stream of integers :

  1. ints() – Returns an unlimited stream of integers. This stream if directly converted to an array would result in java.lang.OutOfMemoryError.
  2. ints(streamSize) – Returns a stream of specified size. This stream when converted to an array will return an array of size equal to the given stream size.
  3. ints(streamSize, lowerBound, upperBound) – Returns a stream of specified size. This stream when converted to an array will create an array of specified size with integers starting from lower bound value and less than upper bound value.
  4. ints(lowerBound, upperBound) – Same as above except the returned stream is unlimited. This stream if directly converted to an array would result in java.lang.OutOfMemoryError.

Generating Array
Code which can be used to generate an array of random integers is :

Random random = new Random();
int[] array = random.ints(10,25,50).toArray();
for (int i = 0; i < array.length; i++) {
   System.out.print(array[i]+" ");
}

The above code first initializes an object of class java.util.Random and then calls the three argument method ints() on this object.
First argument to ints()method is the size of the array to be generated,
Second argument is the lower limit of the array, and
Third argument is the upper limit.

Note that the lower limit is inclusive which means that this digit might be included in the elements of the array while the upper limit is exclusive which means that the elements of the array will be at least 1 less than this number.

Output
Following are the outputs of execution of above lines of code 5 times:

32 25 44 40 31 38 44 43 43 43
43 35 43 33 49 28 26 25 25 42
28 38 31 37 40 44 31 39 42 46
43 44 32 46 32 30 31 41 31 42
49 33 49 35 38 49 48 49 43 42

You can see that the elements of the generated array are different in each run.

Let's tweak in :

  1. An error of type java.lang.IllegalArgumentException: bound must be greater than origin is thrown when the lower and upper limit of the method ints(streamSize, lowerBound, upperBound) are same.
  2. If stream size is not supplied to ints()method then a java.lang.OutOfMemoryError is raised.
  3. Similar to ints()method, there are methods such as longs()and doubles()in java.util.Random class which generate a stream for long and double values respectively.
  4. Elements of the generated array might repeat themselves.
  5. If you want to generate a list instead of an array, modify the code slightly as random.ints(10, 25,50).boxed().collect(Collectors.toList()).
  6. boxed()method of the stream instance returned by the ints() method converts the integer primitives into their wrapper Integer equivalent.


Hit the clap
if the article was useful.

Leave a Reply