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.util.Random

java.util.Random

ints() – Returns an unlimited stream of integers. This stream if directly converted to an array would result in java.lang.OutOfMemoryError . 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. 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. 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 .

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

Generating Array



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 ] + " " ) ; } 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]+" "); }

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

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 :

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. If stream size is not supplied to ints() method then a java.lang.OutOfMemoryError is raised. 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. Elements of the generated array might repeat themselves. 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()) . 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.

1