Java Random.nextInt() – Examples
In this tutorial, we will learn about the Java Random.nextInt() method, and learn how to use this method to generate a random integer value, with the help of examples.
nextInt()
Random.nextInt() returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequence.
Syntax
The syntax of nextInt() method is
Random.nextInt()
Returns
The method returns int value.
Example 1 – nextInt()
In this example, we will create an object random
of Random
class type. We will call nextInt() on this Random object to get the next integer value. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
int i = random.nextInt();
System.out.println("Next random integer value is : " + i);
}
}
Output
Next random integer value is : 759359738
Output may vary, since the integer value is generated randomly.
Example 2 – nextInt()
In this example, we will generate random integers in a for loop using nextInt().
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
for (int i=0; i < 10; i++) {
System.out.println(random.nextInt());
}
}
}
Output
-1606630265
-125238934
1699538591
602193890
-1853930796
-1411396788
358058549
-1257625834
-1537171335
-277970796
nextInt(int bound)
Random.nextInt() Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence.
Syntax
The syntax of nextInt() method with bound specified is
Random.nextInt(int bound)
where
Parameter | Description |
---|---|
bound | The upper bound on the random value to be generated. |
Returns
The method returns int value.
Example 3 – nextInt(int bound)
In this example, we will create an object random
of Random
class type. We will call nextInt(bound) on this Random object to get the next integer value within the number, bound
. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
int bound = 100;
int i = random.nextInt(bound);
System.out.println("Next random integer value is : " + i);
}
}
Output
Next random integer value is : 53
Output may vary, since the integer value is generated randomly.
Example 4 – nextInt(int bound)
In this example, we will generate random integers within the given bound value, in a for loop using nextInt().
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
int bound = 10;
for (int i=0; i < 10; i++) {
System.out.println(random.nextInt(bound));
}
}
}
Output
3
0
6
4
6
1
0
9
3
8
Output may vary, since the integer value is generated randomly.
Conclusion
In this Java Tutorial, we have learnt the syntax of Java Random.nextInt() method, and also learnt how to use this method with the help of examples.