To get a stream of random integers, longs or doubles within a given range – use the `ints()`

, `longs()`

and `doubles()`

methods in `java.util.Random`

.

## Generate Random Number Stream Example

Use below given method calls to get the stream of random number in java applications.

package com.howtodoinjava.java8.streams; import java.util.List; import java.util.Random; import java.util.stream.Collectors; public class RandomNumberExample { public static void main(String[] args) { Random random = new Random(); //Five random integers random.ints( 5 ).sorted().forEach( System.out::println ); //Five random doubles between 0 (inclusive) and 0.5 (exclusive) random.doubles( 5, 0, 0.5 ).sorted().forEach( System.out::println ); //Boxing long to Long so they can be collected List<Long> longs = random.longs( 5 ).boxed().collect( Collectors.toList() ); System.out.println(longs); } }

Output:

-110679835 156507518 1493532761 2054227999 2086798656 7.724902934355127E-4 0.2688148659358355 0.3525387800324165 0.3947437475521328 0.4903926740688234 [-2722967875761490425, 5531368074516893531, -8225300378125625898, -9067114159502460546, -5286357091370510421]

## Methods to get stream of random numbers

Java 8 added several methods to the `Random`

class which can retrun a sequential stream of random numbers. Most widely used methods are:

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

All methods have their overloaded versions as well. e.g. `ints()`

method has these overloaded forms.

//Returns an effectively unlimited stream of pseudorandom int values. IntStream ints() //Each number between the given origin (inclusive) and bound (exclusive). IntStream ints(int randomNumberOrigin, int randomNumberBound) //Returns a stream producing the given streamSize number of pseudorandom int values. IntStream ints(long streamSize) //Returns a stream producing the given streamSize number of pseudorandom int values in given range. IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

Similarly, `longs()`

and `doubles()`

methods also have overloaded versions.

## Generate secure random numbers

To get the stream of secure random numbers (i.e. cryptographically strong random number), use subclass `SecureRandom`

. Any seed material passed to a SecureRandom object must be unpredictable, and all SecureRandom output sequences must be cryptographically strong.

Please note that **all above discussed methods (ints(), longs(), doubles() and their overloads) also work on SecureRandom**, just using

*cryptographically strong pseudo random number generator (*. CSPRNGs 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. .

**CSPRNG**)A true random source is a theoretical random number generator that is perfect in every way. The next value produced is completely uncorrelated with all previous values.

Drop me your questions in comments section.

Reference:

Java 8 Random Class

SecureRandom Class

## Ask Questions & Share Feedback