Java.util.Random.nextInt() in Java

Generating random numbers themselves have a good utility value and having them achieved by the usage of function can prove to be very useful. Java in its language has dedicated an entire library to Random numbers seeing its importance in day-day programming. nextInt() is discussed in this article.

  1. java.util.Random.nextInt() : The nextInt() is used to get the next random integer value from this random number generator’s sequence.

    Declaration : 
    public int nextInt()
    Parameters : 
    NA
    Return Value : 
    The method call returns the next integer number from the sequence
    Exception : 
    NA
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate the working
    // of nextInt()
    import java.util.*;
    public class NextInt1 {
        public static void main(String[] args)
        {
      
            // create random object
            Random ran = new Random();
      
            // generating integer
            int nxt = ran.nextInt();
      
            // Printing the random Number
            System.out.println
            ("The Randomly generated integer is : " + nxt);
        }
    }

    chevron_right

    
    

    Output:

    The Randomly generated integer is : -2052834321
    
  2. java.util.Random.nextInt(int n) : The nextInt(int n) is used to get a random number between 0(inclusive) and the number passed in this argument(n), exclusive.

    Declaration : 
    public int nextInt(int n)
    Parameters : 
    n :  This is the bound on the random number to be returned. Must be positive.
    Return Value : 
    Returns a random number.
    between 0 (inclusive) and n (exclusive).
    Exception : 
    IllegalArgumentException :  This is thrown if n is not positive.
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate the working
    // of nextInt(n)
    import java.util.*;
    public class NextInt2 {
        public static void main(String args[])
        {
      
            // create random object
            Random ran = new Random();
      
            // Print next int value
            // Returns number between 0-9
            int nxt = ran.nextInt(10);
      
            // Printing the random number 
            // between 0 and 9
            System.out.println
            ("Random number between 0 and 10 is : " + nxt);
        }
    }

    chevron_right

    
    

    Output:

    Random number between 0 and 9 is : 4
    
Exception

IllegalArgumentException : This occurs when the argument passed is not positive.
An example to illustrate the Exception generated when n is not a positive number:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstate the Exception
// of nextInt(n)
import java.util.*;
public class NextInt2 {
    public static void main(String[] args)
    {
  
        // create random object
        Random ran = new Random();
  
        // generating number btween 0 and -12345
        // Raises Runtime error, as n is negative.
        int nxt = ran.nextInt(-12345);
  
        System.out.println
        ("Generated Random number is : " + nxt);
    }
}

chevron_right


Runtime Errors:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Random.java:388)
    at NextInt2.main(NextInt2.java:14)

Practical Applications

Generating Random numbers have numerous applications, be it lottery, gambling or small scale gaming. A small Dice game have been demonstrated below in which a 6-mark Dice is thrown by 2 players, person securing 30 points 1st, wins.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstate the Application
// of nextInt(n)
import java.util.*;
public class NextIntAppli {
    public static void main(String[] args)
    {
  
        int sum = 0, sum1 = 0, count = 0, count1 = 0;
        int turn = 0;
  
        // creating random object
        Random ran = new Random();
        int flag = 0;
  
        while (true) {
            if (turn % 2 == 0) {
  
                int p1 = ran.nextInt(6);
                sum += p1;
                System.out.printf
                ("Player 1 after turn %d is : %d\n", turn, sum);
            }
            else {
  
                int p2 = ran.nextInt(6);
                sum1 += p2;
                System.out.printf
                ("Player 2 after turn %d is : %d\n", turn, sum1);
            }
            if (sum >= 30) {
                flag = 1;
                break;
            }
            if (sum1 >= 30) {
                flag = 2;
                break;
            }
            turn++;
        }
        if (flag == 1)
            System.out.println("\nPlayer 1 WON!!");
        else
            System.out.println("\nPlayer 2 WON!!");
    }
}

chevron_right


Output:

Player 1 after turn 0 is : 0
Player 2 after turn 1 is : 4
Player 1 after turn 2 is : 2
Player 2 after turn 3 is : 9
Player 1 after turn 4 is : 5
Player 2 after turn 5 is : 9
Player 1 after turn 6 is : 6
Player 2 after turn 7 is : 14
Player 1 after turn 8 is : 8
Player 2 after turn 9 is : 18
Player 1 after turn 10 is : 12
Player 2 after turn 11 is : 21
Player 1 after turn 12 is : 13
Player 2 after turn 13 is : 26
Player 1 after turn 14 is : 18
Player 2 after turn 15 is : 29
Player 1 after turn 16 is : 18
Player 2 after turn 17 is : 34
Player 2 WON!!

This article is contributed by Suman Singh Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : TomCalver



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.