Random class Java 8 Methods(ints(), longs(), doubles()) with 12 examples.

1. Overview

In this article, I will go through the new methods added in Random class under java.util package. Random class provides several methods that generate pseudo-random numbers. The new methods added in Random class can generate the int, long or double stream of pseudo-random numbers. 

2. Random class new methods

Method NameDescription
Public IntStream ints(long streamSize)Returns a IntStream of size streamSize with pseudo-random int values
public IntStream ints()Returns effectively unlimited IntStream of pseudo-random int values. This method is equivalent to ints(Long.MAX_VALUE)
public IntStream ints(
long streamSize, 
int randomNumberOrigin,
int randomNumberBound)
This method returns IntSteam of size streamSize with pseudo-random int values. Each value generated will conform to an origin(inclusive) and bound(exclusive).
public IntStream ints(
int randomNumberOrigin,
int randomNumberBound)
This method is like int() or int(Long.MAX_VALUE) as it generates an effectively unlimited number of pseudo-random numbers. Each value generated will conform to an origin(inclusive) and bound(exclusive).
public LongStream longs(
long streamSize)
Returns a LongStream of size streamSize with pseudo-random long values
public LongStream longs()This method returns a LongStream with an effectively unlimited stream of pseudo-random long values. This method is equivalent to method longs(streamSize) as the upper bound of the long values generated is equal to Long.MAX_VALUE i.e. longs() is equivalent to longs(Long.MAX_VALUE)
public LongStream longs(
long streamSize, 
long randomNumberOrigin, 
long randomNumberBound)
This method returns LongSteam of size streamSize with pseudo-random long values. Each value generated will conform to an origin(inclusive) and bound(exclusive).
public LongStream longs(
long randomNumberOrigin,
long randomNumberBound)
This method is like longs() or longs(Long.MAX_VALUE) as it generates an effectively unlimited number of Pseudo-random long numbers. Each value generated will conform to an origin(inclusive) and bound(exclusive). 
public DoubleStream doubles(
long streamSize)
Returns a DoubleStream of size streamSize with pseudo-random double values
public DoubleStream doubles()This method returns a DoubleStream with an effectively unlimited stream of pseudo-random double values. This method is equivalent to method doubles(streamSize) as the upper bound of the double values generated is equal to Long.MAX_VALUE i.e. doubles() is equivalent to doubles(Long.MAX_VALUE)
public DoubleStream doubles(
long streamSize, 
double randomNumberOrigin, double randomNumberBound)
This method returns a DoubleSteam of size streamSize with pseudo-random double values. Each value generated will conform to an origin(inclusive) and bound(exclusive).
public DoubleStream doubles(
double randomNumberOrigin,
double randomNumberBound)
This method is like doubles() or doubles(Long.MAX_VALUE) as it generates an effectively unlimited number of pseudo-random doubles numbers. Each value generated will conform to an origin(inclusive) and bound(exclusive). 

3. Generating int random number

3.1 public IntStream ints(long streamSize)

This method returns IntSteam of size streamSize with pseudo-random int values. If the streamSize is less than zero, then the method throws IllegalArgumentException.

	/**
	 * Generates a stream with 10 int pseudo-random values.
	 * */
	Random random = new Random();
	IntStream intStream = random.ints(10);
	Assert.assertEquals(10, intStream.count());

3.2 public IntStream ints(
long streamSize,
int randomNumberOrigin,
int randomNumberBound)

This method returns IntSteam of size streamSize with pseudo-random int values. Each value generated will conform to an origin(inclusive) and bound(exclusive). 

If the streamSize is less than zero or randomNumberOrigin is greater than or equal to randomNumberBound, then the method throws IllegalArgumentException.

	/**
	 * Generates stream with 10 int pseudo-random int values
	 * from 1(inclusive) to 5(exclusive)
	 * */
	Random random = new Random();
	IntStream intStream = random.ints(10, 1, 5); 
	//Sample output 1 3 2 2 3 3 1 3 1 4 
	Assert.assertEquals(10, intStream.count());

3.3 public IntStream ints()

This method returns an IntStream with an effectively unlimited stream of pseudo-random int values. This method is equivalent to method ints(streamSize) as the upper bound of the int values generated is equal to Long.MAX_VALUE i.e. ints() is equivalent to ints(Long.MAX_VALUE)

If you don’t have a use-case to use this version, then use a version of ints() method that is size bound.

	/**
	 * Generates an unlimited stream of pseudo-random int values.
	 * */
	Random random = new Random();
	IntStream intStream = random.ints();

3.4 public IntStream ints(
int randomNumberOrigin,
int randomNumberBound)

This method is like int() or int(Long.MAX_VALUE) as it generates an effectively unlimited number of pseudo-random numbers. Each value generated will conform to an origin(inclusive) and bound(exclusive). 

If the randomNumberOrigin is greater than or equal to randomNumberBound, then the method throws IllegalArgumentException.

	/**
	 * Generates unlimited stream of pseudo-random int values 
	 * from 10(inclusive) to 10_000(exclusive)
	 * */
	Random random = new Random();
	IntStream intStream = random.ints(10, 10_000);

4. Generating long random number

4.1 public LongStream longs(long streamSize)

This method returns LongSteam of size streamSize with pseudo-random long values. If the streamSize is less than zero, then the method throws IllegalArgumentException.

	/**
	 * Generates a stream with 10 long pseudo-random values.
	 * */
	Random random = new Random();
	LongStream longStream = random.longs(10); 
	Assert.assertEquals(10, longStream.count());

4.2 public LongStream longs(
long streamSize, 
long randomNumberOrigin,
long randomNumberBound)

This method returns LongSteam of size streamSize with pseudo-random long values. Each value generated will conform to an origin(inclusive) and bound(exclusive). 

	/**
	 * Generates stream with 10 long pseudo-random values
	 * from 1(inclusive) to 5(exclusive)
	 * */
	Random random = new Random();
	LongStream longStream = random.longs(10, 1, 5); 
	// Sample output 4 3 3 3 4 4 2 2 4 2
	Assert.assertEquals(10, longStream.count());

If the streamSize is less than zero or randomNumberOrigin is greater than or equal to randomNumberBound, then the method throws IllegalArgumentException.

4.3 public LongStream longs()

This method returns a LongStream with an effectively unlimited stream of pseudo-random long values. This method is equivalent to method longs(streamSize) as the upper bound of the long values generated is equal to Long.MAX_VALUE i.e. longs() is equivalent to longs(Long.MAX_VALUE).

	/**
	 * Generates an unlimited stream of pseudo-random long values.
	 * */
	Random random = new Random();
	LongStream longStream = random.longs();
	Assert.assertNotNull(longStream);

If you don’t have a use-case to use this version, then use a version of longs() method that is size bound.

4.4 public LongStream
longs(long randomNumberOrigin, long randomNumberBound)

This method is like longs() or longs(Long.MAX_VALUE) as it generates an effectively unlimited number of pseudo-random long numbers. Each value generated will conform to an origin(inclusive) and bound(exclusive). 

If the randomNumberOrigin is greater than or equal to randomNumberBound, then the method throws IllegalArgumentException.

	/**
	 * Generates unlimited stream of pseudo-random long values 
	 * from 10(inclusive) to 10_000(exclusive)
	 * */
	Random random = new Random();
	LongStream longStream = random.longs(10, 10_000);
	Assert.assertNotNull(longStream);

5. Generating double random number

5.1 public DoubleStream doubles(long streamSize)

This method returns a DoubleSteam of size streamSize with pseudo-random double values. If the streamSize is less than zero, then the method throws IllegalArgumentException.

	/**
	 * Generates a stream with 10 double pseudo-random values.
	 * */
	Random random = new Random();
	DoubleStream doubleStream = random.doubles(10);
	Assert.assertEquals(10, doubleStream.count());

5.2 public DoubleStream doubles(
long streamSize, 
double randomNumberOrigin,
double randomNumberBound)

This method returns a DoubleSteam of size streamSize with pseudo-random double values. Each value generated will conform to an origin(inclusive) and bound(exclusive). 

If the streamSize is less than zero or randomNumberOrigin is greater than or equal to randomNumberBound, then the method throws IllegalArgumentException.

	/**
	 * Generates stream with 10 long pseudo-random values
	 * from 1(inclusive) to 5(exclusive)
	 * */
	Random random = new Random();
	DoubleStream doubleStream = random.doubles(10, 1, 5);
	Assert.assertEquals(10, doubleStream.count());

5.3 public DoubleStream doubles()

This method returns a DoubleStream with an effectively unlimited stream of pseudo-random double values. This method is equivalent to method doubles(streamSize) as the upper bound of the double values generated is equal to Long.MAX_VALUE i.e. doubles() is equivalent to doubles(Long.MAX_VALUE)

	/**
	 * Generates an unlimited stream of pseudo-random long values.
	 * */
	Random random = new Random();
	DoubleStream doubleStream = random.doubles();

If you don’t have a use-case to use this version, then use the doubles() method that is size bound.

5.4 public DoubleStream doubles(
double randomNumberOrigin,
double randomNumberBound)

This method is like doubles() or doubles(Long.MAX_VALUE) as it generates an effectively unlimited number of pseudo-random doubles numbers. Each value generated will conform to an origin (inclusive) and bound(exclusive). 

If the randomNumberOrigin is greater than or equal to randomNumberBound, then the method throws IllegalArgumentException.

	/**
	 * Generates unlimited stream of pseudo-random long values 
	 * from 10(inclusive) to 10_000(exclusive)
	 * */
	Random random = new Random();
	DoubleStream doubleStream = random.doubles(10, 10_000);

6. Conclusion

In this article, we learned new methods of Random class with examples. Using the size bound method/s is preferable as it will generate a definite number of elements on which we can work on. Use unsized methods only when your use-case is suitable for it.

Leave a Reply

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