Stream of Random Numbers in Java

Learn to get a Stream of random numbers in Java using the Random and SecureRandom classes.

1. The Random API

Java 8 release has added several methods to the Random class which can return a sequential stream of random numbers (integers, longs and doubles). The most widely used methods are:

  • IntStream ints()
  • LongStream longs()
  • DoubleStream doubles()

All of the above methods have their overloaded forms. For example, ints() method has these overloaded forms.

  • IntStream ints() – Returns an effectively unlimited stream of pseudorandom int values.
  • IntStream ints(long streamSize) – Returns a stream producing the given number of pseudorandom int values.
  • IntStream ints(int origin, int bound) – Returns an effectively unlimited stream of int values, each conforming to the given origin (inclusive) and bound (exclusive).
  • IntStream ints(long streamSize, int origin, int bound) – Returns a stream producing the given number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

2. Example of Stream of Random Numbers

Let’s learn to use the above-discussed methods to create a stream of random numbers.

Random random = new Random();

IntStream randStream = random.ints(5);

DoubleStream doubleStream = random.doubles(5, 0, 0.5);

//collect to list
List<Long> longs = random.longs(5)

3. Stream of Secure Random Numbers

To get the stream of secure random numbers (i.e. cryptographically strong random number), use the subclass SecureRandom. By default, SecureRandom uses the SHA1PRNG algorithm. The default constructor also uses this algorithm.

CSPRNG (cryptographically strong pseudo-random number generator) uses entropy, which is nothing but an unpredictable input (true random source). It might be a hardware random number generator or possibly some unpredictable system process, such as the timings events, interrupts etc.

SecureRandom secureRandomGenerator = SecureRandom.getInstanceStrong();
//1. Get 128 random bytes
byte[] randomBytes = new byte[128];
//2. Get random integer
int r = secureRandomGenerator.nextInt();
//3. Get random integer in range
int randInRange = secureRandomGenerator.nextInt(999999);

Please note that all the above-discussed methods (ints(), longs(), doubles() and their overloads) also work with the SecureRandom class.

SecureRandom sRand = SecureRandom.getInstanceStrong();

IntStream randStream = sRand.ints(5);


Happy Learning !!

Sourcecode on Github


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode