How to generate a stream of random numbers in Java
From time to time you end up with the need of generating some random numbers. In Java, the
Random class has been the go-to solution for this type of work.
Random serves several helpful methods for creating random ints, doubles and longs. However, if you wanted a sequence of random numbers in the pre-Java 8 era, you had to combine
Random with a traditional for-loop. This is a bit verbose for such a simple job.
Luckily with the introduction of streams in Java 8,
Random was not forgotten. The
Random class was extended to contain methods for generating streams of random ints, doubles and longs!
Enough chatting — let's create some random streams!
Generating an infinite stream of random ints
Let's start off our examples by generating a simple stream of ints.
new Random().ints(); // returns an IntStream
That's it! Our first stream of random ints! With that said, be careful — this will create an infinite stream. For instance, converting an infinite stream into a list would make our code blow up.
That's why you may need some way of limiting the size of the stream.
You could of course limit the stream size by using one of the methods the Stream API got to offer — like
limit — but instead you could send a parameter to
Random.ints that will set the size of the stream.
Now our random stream will only contain 5 elements.
Setting a range for the random numbers
One issue we haven't looked at is that we usually want to specify a range that the random ints should be within. If we don't specify such a range, we'll get all sorts of crazy ints in our stream.
// random int stream without range specified new Random().ints(); //> -1366209626, 2042421164, -1164519030, 515921421 (...)
As with the size, we can simply specify the range by sending them as parameters to
To show this, say we want a random stream of numbers that should be within the range of 0 to 10.
new Random().ints(0, 11); //> 5, 5, 10, 8, 4 (...)
Now you may ask — why set the upper limit to 11? Well, the thing is that the lower limit is inclusive the given number while the upper limit is exclusive the given number.
Combining size and range
Now that we know how to set the size and range, we can combine them by sending all of them as parameters to
new Random().ints(5, 0, 11);
Thats it! This will result in a stream of length 5 containing random integers within the range of 0 to 10.
Creating streams of doubles and longs
Now that we've seen how we can create different types of streams of random ints, we can just as easily create doubles and longs.
The same variants of the
Random.ints method can be found as
For instance, if we take the last
Random.ints example and change it to
doubles, we'll get a
DoubleStream with size 5 and double values within the range of 0 and 10.
new Random().doubles(5,0,11); //>1.6474791969126263, 10.309938068348462, 7.112206995829152, 6.096409733914366, 6.834149261438854
Same goes for
Random.longs — only it'll create a LongStream.
new Random().longs(5,0,11); //> 0, 6, 7, 10, 10