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
java_util_random.htm
Advertisements