What is the Random.ints method in Java?
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 classRandom random=new Random();// stream sizelong streamSize = 10;// using ints generate stream of random intsrandom.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 classRandom random=new Random();// using ints generate stream of random intsrandom.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 classRandom random=new Random();// size of the streamint streamSize = 8;// origin of each random numberint randomNumberOrigin = 1;// bound of each random numberint randomNumberBound = 7;// Stream of random ints generatedIntStream ds = random.ints(streamSize, randomNumberOrigin, randomNumberBound);// print the generated random valuesds.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 classRandom random=new Random();// origin of each random numberint randomNumberOrigin = 1;// bound of each random numberint randomNumberBound = 7;// Stream of random ints generatedIntStream ds = random.ints(randomNumberOrigin, randomNumberBound);// print the generated random valuesds.limit(10).forEach(System.out::println);}}