- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
java Random nextInt() Method
Description
The java Random nextInt() method is used to get the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
Declaration
Following is the declaration for java.util.Random.nextInt() method.
public int nextInt()
Parameters
NA
Return Value
The method call returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
Exception
NA
java Random nextInt(int n) Method
Description
The java Random nextInt(int n) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
Declaration
Following is the declaration for java.util.Random.nextInt() method.
public int nextInt(int n)
Parameters
n − This is the bound on the random number to be returned. Must be positive.
Return Value
The method call returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
Exception
IllegalArgumentException − This is thrown if n is not positive.
Getting a Random Integer Value Example
The following example shows the usage of Java Random nextInt() method. Firstly, we've created a Random object and then using nextInt() we retrieved a random integer and printed it.
package com.tutorialspoint; import java.util.Random; public class RandomDemo { public static void main( String args[] ) { // create random object Random randomNo = new Random(); // get next next pseudorandom value int value = randomNo.nextInt(); // check the value System.out.println("Value is: " + value); } }
Output
Let us compile and run the above program, this will produce the following result.
Value is: 125690894
Getting a Random Integer Value With Given Seed Example
The following example shows the usage of Java Random nextInt() method. Firstly, we've created a Random object with a seed value and then using nextInt() we retrieved a random integer and printed it.
package com.tutorialspoint; import java.util.Random; public class RandomDemo { public static void main( String args[] ) { // create random object Random randomNo = new Random(10); // get next next pseudorandom value int value = randomNo.nextInt(); // check the value System.out.println("Value is: " + value); } }
Output
Let us compile and run the above program, this will produce the following result.
Value is: -1157793070
Getting a Random Integer Value of Given Bound With a Seed Example
The following example shows the usage of Java Random nextInt(int) method. Firstly, we've created a Random object with a seed value and then using nextInt(int) we retrieved a random integer within given bound and printed it.
package com.tutorialspoint; import java.util.Random; public class RandomDemo { public static void main( String args[] ) { // create random object Random randomNo = new Random(10); // get next next pseudorandom value int value = randomNo.nextInt(20); // check the value System.out.println("Value is: " + value); } }
Output
Let us compile and run the above program, this will produce the following result.
Value is: 13
To Continue Learning Please Login
Login with Google