Java > Generate a lot of int

In Java 8 streams, you can generate a stream of integers between two numbers using:

IntStream.range(lower, upper)...

If you want them to be randomised, then you can use:

Random random = new Random();
random.ints(count, lower, upper)...

You can then use methods such as forEach, reduce or collect to do something with the stream.

So, for example, random.ints(1000, 1, 100).forEach(i -> doSomething(i)) will generate 1000 random numbers between 1 and 99 and call doSomething on each of them.

Leave a Comment