Java Utility Library

Java Random - ints() Method



The java.util.Random.ints() method returns an effectively unlimited stream of pseudorandom int values. A pseudorandom int value is generated as if it's the result of calling the method nextInt().

Syntax

public IntStream ints()

Parameters

No parameter is required.

Return Value

Returns a stream of pseudorandom int values.

Exception

NA

Example:

In the example below, the java.util.Random.ints() method is used to get a stream of pseudorandom int values.

import java.util.*;
import java.util.stream.IntStream;

public class MyClass {
  public static void main(String[] args) {
    //creating a random object
    Random rand = new Random();

    //generating a stream of int random numbers
    IntStream stream = rand.ints();

    //printing 10 random numbers from the stream
    stream.limit(10).forEach(System.out::println); 
  }
}

One of the possible outcome is given below:

-72639393
437248104
727662717
2033696104
807129702
1676581163
1153472005
-970428822
39898007
-564411195

❮ Java.util - Random