What is the Random.ints method in Java?

Share

ints is an instance method of the Random class that is used to generate a stream of random integers. There are four different variants of this method, namely:

  • ints(long streamSize)
  • ints()
  • ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
  • ints(int randomNumberOrigin, int randomNumberBound)

The Random class is defined in the java.util package. To import the Random class, we check the following import statement:

import java.util.Random;

ints(long streamSize)

The ints(long streamSize) method is used to generate a stream of random integer values, which is of the specified size.

Syntax

public IntStream ints(long streamSize)

Parameters

  • streamSize: This is the size of the stream that needs to be generated.

Return value

This method returns a stream of random integer values, which is of the specified size.

Code

import java.util.Random;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// stream size
long streamSize = 10;
// using ints generate stream of random ints
random.ints(streamSize).forEach(System.out::println);
}
}

ints()

The ints() method is used to generate an infinite stream of random integer values.

Syntax

public IntStream ints()

Parameters

This method takes no parameters.

Return value

An infinite stream of random integer values is returned.

Code

import java.util.Random;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// using ints generate stream of random ints
random.ints().limit(4).forEach(System.out::println);
}
}

ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

This method is used to generate a stream of pseudorandom integer values, which is of the specified size. The specified size is bounded by a lower bound (inclusive) and an upper bound (exclusive).

Syntax

public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

Parameters

  • streamSize: This is the size of the stream that needs to be generated.

  • randomNumberOrigin: This is the lower bound (inclusive) for each generated value.

  • randomNumberBound: This is the upper bound (exclusive) for each generated value.

Return value

This method returns a stream of pseudorandom integer values. This stream is of the specified size, and adheres to the lower (inclusive) and upper (exclusive) bounds.

Code

import java.util.Random;
import java.util.stream.IntStream;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// size of the stream
int streamSize = 8;
// origin of each random number
int randomNumberOrigin = 1;
// bound of each random number
int randomNumberBound = 7;
// Stream of random ints generated
IntStream ds = random.ints(streamSize, randomNumberOrigin, randomNumberBound);
// print the generated random values
ds.forEach(System.out::println);
}
}

ints(int randomNumberOrigin, int randomNumberBound)

This method is used to generate an infinite stream of pseudorandom integer values, each of which adheres to the provided lower bound (inclusive) and upper bound (exclusive).

Syntax

public IntStream ints(int randomNumberOrigin, int randomNumberBound)

Parameters

  • randomNumberOrigin: This is the lower bound (inclusive) for each generated value.

  • randomNumberBound: This is the upper bound (exclusive) for each generated value.

Return value

This method returns an infinite stream of pseudorandom integer values, each of which dheres to the provided lower bound (inclusive) and upper bound (exclusive).

Code

import java.util.Random;
import java.util.stream.IntStream;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// origin of each random number
int randomNumberOrigin = 1;
// bound of each random number
int randomNumberBound = 7;
// Stream of random ints generated
IntStream ds = random.ints(randomNumberOrigin, randomNumberBound);
// print the generated random values
ds.limit(10).forEach(System.out::println);
}
}