Java Utility Library

Java Random - ints() Method



The java.util.Random.ints() method returns a stream producing the given streamSize number 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(long streamSize)

Parameters

streamSize Specify the number of values to generate.

Return Value

Returns a stream of pseudorandom int values.

Exception

Throws IllegalArgumentException, if streamSize is less than zero.

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 containing 10
    //int random numbers
    IntStream stream = rand.ints(10);

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

One of the possible outcome is given below:

-501472440
-1672827373
-362328820
-2021951285
737200492
1170979594
605467437
1367535207
-1712521668
120143357

❮ Java.util - Random