Skip to main content

Generate Random Numbers Java Stream

If you are writing a Java program to generate random numbers two tried and tested ways are to either use Math.random() method or methods of Random class to get a random number but the limitation with these methods is that they generate a single random number and you would need to use them in a loop in case you need a list of random numbers. With the introduction of Java Stream API in Java 8, Random class has also been expanded to include methods that can return a stream of ints, longs or doubles which means a return type of IntStream, LongStream or DoubleStream.

Random class methods for generating streams

In Random class there are following methods for generating infinite streams-

1. ints()- Returns an effectively unlimited stream of int values.

2. ints(int randomNumberOrigin, int randomNumberBound)- Returns an effectively unlimited stream of int values within the given bounds.

Following methods restrict the generated random numbers.

1. ints(long streamSize)- Returns a stream producing the given streamSize number of int values.

2. ints(long streamSize, int randomNumberOrigin, int randomNumberBound)- Returns a stream producing the given streamSize number of int values within the given bounds.

Same set of methods also exist for producing long and double random numbers-

  • longs()
  • longs(long streamSize)
  • longs(long randomNumberOrigin, long randomNumberBound)
  • longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
and
  • doubles()
  • doubles(double randomNumberOrigin, double randomNumberBound)
  • doubles(long streamSize), doubles(long streamSize, double randomNumberOrigin
  • double randomNumberBound)

Generating stream of random number examples

1. An infinite stream of integers using ints() method of the Random class. Here limit() method of Java Stream is used to limit the stream.

import java.util.Random;

public class RandomNumberGeneration {
  public static void main(String[] args) {
    Random random = new Random();
    random.ints().limit(10).forEach(System.out::println);
  }
}
Output
164843204
-1469424678
1335628408
29431696
267957743
-944667359
228878324
672262783
1504662080
-262691321

2. In this example we’ll generate 10 random numbers in between the lower and upper bounds of 10 and 50.

public class RandomNumberGeneration {
  public static void main(String[] args) {
    Random random = new Random();
    random.ints(10, 10, 51).forEach(System.out::println);
  }
}
Output
39
29
14
49
26
29
37
50
31
48

Note that with in the passed bounds lower bound is inclusive where as upper bound is exclusive that’s why to make upper bound till 50, 51 is passed as the argument.

3. If you want 10 random doubles with in the range 0 and 1.

public class RandomNumberGeneration {
  public static void main(String[] args) {
    Random random = new Random();
    random.doubles(10, 0, 1).forEach(System.out::println);
  }
}
Output
0.6099718485028252
0.3440097793096719
0.31985736196344106
0.6028702735888255
0.8780031623608885
0.09055972507136933
0.8280686637964826
0.7917602864784455
0.7277181639918716
0.8424139111003316

That's all for the topic Generate Random Numbers Java Stream. If something is missing or you have something to share about the topic please write a comment.


You may also like