Java.util.Random.nextInt() in Java (original) (raw)

Last Updated : 21 Mar, 2025

Generating random numbers themselves has a good utility. Java provides a method Random.nextInt() which is the part of Random Class present in the util package. The **nextInt() method is used to get the random integer values in the range of int.

Syntax

int nextInt()

int nextInt(int bound)

int nextInt(int origin, int bound)

**Parameters:

**Return Type:

**Exception:

**Example 1: Generating random numbers without any bound using the nextInt() method of Random Class.

Java `

// Java Program to generate random numbers // using nextInt() method of Random class

import java.util.Random;

public class Geeks { public static void main(String[] args) { // create an object of Random class Random r = new Random();

    // Generate random integers without any bounds
    System.out.println("Random number: " + r.nextInt());
}

}

`

Output

Random number: 599611746

**Explanation: This method gives random numbers which could be either negative or positive integers from range **Integer.MIN_VALUE to **Integer.MAX_VALUE, whenever we run the program we get a different output.

**Example 2: Generating random numbers using int nextInt(int bound).

Java `

// Java code to demonstrate the usage of Random.nextInt(int bound)

import java.util.*;

public class Geeks {

public static void main(String[] args) {

    // create random object 
    Random r = new Random();

    // generating number between 0 (inclusive) and 10 (exclusive)
    int nxt = r.nextInt(10);

    System.out.println("Generated Random number is : " + nxt);
}

}

`

Output

Random number between 0 and 10 is : 4

**Explanation: In the above code example we use the Random.nextInt(int bound) method and pass an int value 10 as an argument as bound and it returns a value from 0 to 10

**Example 3: Creating a dice game logic using the nextInt() method to show real-life uses of random numbers.

Java `

import java.util.Random;

public class Geeks {

public static void main(String[] args) {

    // Player Score variables
    int p1 = 0, p2 = 0;

    // Game variable to count the number of turns
    int turns = 0;

    // Score to win the game
    int Win = 5;
    Random random = new Random();

    // Game loop, players alternate turns
    while (p1 < Win && p2 < Win) {
        turns++;

        // Player 1's turn (even turns)
        if (turns % 2 == 1) {

            // Dice roll between 1 and 6
            int p1Roll = random.nextInt(6) + 1;
            p1 += p1Roll;
            System.out.printf("Player 1 after turn %d: Roll = %d, Total Score = %d\n", turns, p1Roll, p1);
        } // Player 2's turn (odd turns)
        else {

            // Dice roll between 1 and 6
            int p2Roll = random.nextInt(6) + 1;
            p2 += p2Roll;
            System.out.printf("Player 2 after turn %d: Roll = %d, Total Score = %d\n", turns, p2Roll, p2);
        }
    }

    // Calculating the winner of the game
    if (p1 >= Win) {
        System.out.println("\nPlayer 1 WON!!");
    } else {
        System.out.println("\nPlayer 2 WON!!");
    }
}

}

`

Output

Player 1 after turn 1: Roll = 1, Total Score = 1 Player 2 after turn 2: Roll = 4, Total Score = 4 Player 1 after turn 3: Roll = 6, Total Score = 7

Player 1 WON!!

**Explanation: In the above example we use the Random.nextInt() method to create a real-world example of random numbers in this example we set a winning score and the two players can roll a die one by one if any of the player's total count is equal or greater than the max than that player wins the game.

**Example 4: illustrate the Exception generated in Random.nextInt(int bound) when bound is not a positive number

Java `

// Java code to demonstrate the Exception // in Random.nextInt(int bound )

import java.util.*;

public class Geeks { public static void main(String[] args) { // create random object Random r = new Random();

    // generating number between 0 and -12345 
    // Raises Runtime error, as bound is negative. 
    int nxt = r.nextInt(-12345);

    System.out.println("Generated Random number is : " + nxt);
}

}

`

**Output:

Exception

**Explanation: In this program, we try to generate the random numbers but we pass a negative bound which will throw the exception as shown in the image output.

**Important Points