java.util.Random.nextInt() Method



Description

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

Example

The following example shows the usage of java.util.Random.nextInt()

package com.tutorialspoint;

import java.util.*;

public class RandomDemo {
   public static void main( String args[] ) {

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

      // check next int value  
      System.out.println("Next int value: " + randomno.nextInt(10000));
   }    
}

Let us compile and run the above program, this will produce the following result.

Next int value: 2110
java_util_random.htm
Advertisements