Java Random.ints() – Examples
In this tutorial, we will learn about the Java Random.ints() method, and learn how to use this method to generate a stream of pseudorandom int
values, with the help of examples.
ints()
Random.ints() returns an effectively unlimited stream of pseudorandom int values.
Syntax
The syntax of ints() method is
Random.ints()
Returns
The method returns IntStream object.
Example 1 – ints()
In this example, we will generate an unlimited sequence of random integers using ints() method and print out four of them to the console.
Java Program
import java.util.Random;
import java.util.stream.IntStream;
import java.util.function.IntConsumer;
public class Example {
public static void main(String[] args) {
Random random = new Random();
IntStream ds = random.ints();
ds.limit(4).forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
1549243955
-654704097
-1217687007
1648661790
ints(randomNumberOrigin, randomNumberBound)
Random.ints() returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
Syntax
The syntax of ints() method with random number origin and random number bound is
Random.ints(int randomNumberOrigin, int randomNumberBound)
where
Parameter | Description |
---|---|
randomNumberOrigin | The origin (inclusive) of each random value. |
randomNumberBound | The bound (exclusive) of each random value. |
Returns
The method returns IntStream object.
Example 2 – ints(randomNumberOrigin, randomNumberBound)
In this example, we will generate an unlimited sequence of random integers which are limited by an origin and bound using ints() method and print out five of these numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
public class Example {
public static void main(String[] args) {
int randomNumberOrigin = 1;
int randomNumberBound = 7;
Random random= new Random();
IntStream ds = random.ints(randomNumberOrigin, randomNumberBound);
ds.limit(5).forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
2
2
1
1
5
ints(long streamSize)
Random.ints() returns a stream producing the given streamSize number of pseudorandom int values.
Syntax
The syntax of ints() method with stream size is
Random.ints(long streamSize)
where
Parameter | Description |
---|---|
streamSize | The number of values to generate in the stream. |
Returns
The method returns IntStream object.
Example 3 – ints(streamSize)
In this example, we will generate eight random integers using ints(streamSize) method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
public class Example {
public static void main(String[] args) {
int streamSize = 8;
Random random = new Random();
IntStream ds = random.ints(streamSize);
ds.forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
790793602
-445498747
-322809516
1575745272
732465345
586364815
1791511337
-1366525847
ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Random.ints() Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
Syntax
The syntax of ints() method with stream size, random number origin and random number bound is
Random.ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
where
Parameter | Description |
---|---|
streamSize | The number of values to generate in the Stream. |
randomNumberOrigin | The origin (inclusive) of each random value in the Stream. |
randomNumberBound | The bound (exclusive) of each random value in the Stream. |
Returns
The method returns IntStream object.
Example 4 – ints(streamSize, randomNumberOrigin, randomNumberBound)
In this example, we will generate eight random integers which are limited by an origin 1
and bound 7
using ints() method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
public class Example {
public static void main(String[] args) {
int streamSize = 8;
int randomNumberOrigin = 1;
int randomNumberBound = 7;
Random random = new Random();
IntStream ds = random.ints(streamSize, randomNumberOrigin, randomNumberBound);
ds.forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
4
6
2
5
5
3
3
2
Conclusion
In this Java Tutorial, we have learnt the syntax of Java Random.ints() method, and also learnt how to use this method with the help of Java Examples.