VOOZH about

URL: https://www.geeksforgeeks.org/java/java-util-random-nextint-java/

⇱ Java.util.Random.nextInt() in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java.util.Random.nextInt() in Java

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:

  • bound(Optional): It takes an integer number of type int and returns a random value between 0 (inclusive) and bound (exclusive) value.
  • origin(Optional): Origin is the starting parameter and returns random numbers from origin (inclusive) and bound (exclusive).

Return Type:

  • This method returns random integer values.

Exception:

  • IllegalArgumentException: Throw this exception when the argument passed is not positive or the origin is greater than or equal to bound.

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


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).


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.


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

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

  • Range: This method returns an integer value in the range of in which is from -2^31 to 2^31 - 1.
  • ThreadLocalRandom: For multithreaded environments, it's better to use the ThreadLocalRandom class which is thread-safe.
  • java.security.SecureRandom: Although this method gives the random integer values for better security we can use the alternative SecureRandom which returns the cryptographically secure random numbers.


Comment
Article Tags: