Java Utility Library

Java Random - ints() Method



The java.util.Random.ints() method returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

Syntax

public IntStream ints(int randomNumberOrigin,
                      int randomNumberBound)

Parameters

randomNumberOrigin Specify the origin (inclusive) of each random value.
randomNumberBound Specify the bound (exclusive) of each random value.

Return Value

Returns a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive).

Exception

Throws IllegalArgumentException, if randomNumberOrigin is greater than or equal to randomNumberBound.

Example:

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

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 int random
    //numbers between 500(inclusive) and 1000(exclusive)
    IntStream stream = rand.ints(500, 1000);

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

One of the possible outcome is given below:

995
965
775
829
747
510
658
601
952
905

❮ Java.util - Random