CodeGym/Java Blog/Methods in Java/Java Random nextInt() Method
Author
Jesse Haniel
Lead Software Architect at Tribunal de Justiça da Paraíba

Java Random nextInt() Method

Published in the Methods in Java group
members
There are several options for creating a pseudo-random number generator in Java language. One of these options is the use of the java.util.Random class and the nextInt() method. In this article, we are going to talk about Random nextInt() method and give some code examples of its use.

Briefly about the java.util.Random class

The java.util.Random class, as we said, is a pseudo-random number generator. The class is represented by two constructors
  • Random() — creates a number generator using a unique seed

  • Random(long seed) — allows you to specify the seed manually

Since the class creates a pseudo-random number, by specifying a seed, you determine the starting point of the random sequence. And you will get the same random sequences. To avoid such a match, it is common to use a second constructor using the current time as the initial value.

Random nextInt() method

There are two options java.util.Random.nextInt() Method
  • int nextInt(int n) — returns the next random value of type int in the range from 0 to n. The method throws IllegalArgumentException, if n isn't positive.

  • int nextInt() — returns the next random int value

Random nextInt() method code Example

Let’s try both variants of java.util.Random.nextInt() Method with code examples. Here is an example of nextInt() method without arguments:
import java.util.*;
public class RandomTest {
   public static void main(String[] args)
   {
       //creating a Random Object ran
       Random ran = new Random();

       //generating a number using nextInt() method
       int randomNumber = ran.nextInt();

       System.out.println("Randomly generated number = " + randomNumber);
   }
}
The output will be…we don't know for sure! Just try the code and you'll get a randomly generated integer number. Let's try out Random.nextInt() with an argument. However, this time our problem will be more interesting. You have probably seen online casino ads more than once, they literally flooded the Internet. So, the number generator's sequence in such games is usually created using methods and classes like Random. Let's create a program in which we roll two dice with a number from 1 to 6 written on each side.
import java.util.*;
public class RandomTest2 {
   public static void main(String args[])
   {

       // create Random Object
       Random random = new Random();

       // Printing the 6 random numbers between 1 and 6 using //random.nextInt()


      for (int i = 1; i < 7; i++) {
          System.out.println("throwing a dice for the " + i + " time");
       System.out.println ("Random number between 1 and 6 is = " + (1 + random.nextInt(6)));
   }
   }
}
In this program, the player "rolls" the dice 6 times in a row. Random.nextInt() determines the next number. Here is one of the results:
throwing a dice for the 1 time Random number between 1 and 6 is = 5 throwing a dice for the 2 time Random number between 1 and 6 is = 6 throwing a dice for the 3 time Random number between 1 and 6 is = 6 throwing a dice for the 4 time Random number between 1 and 6 is = 5 throwing a dice for the 5 time Random number between 1 and 6 is = 2 throwing a dice for the 6 time Random number between 1 and 6 is = 4
In a similar way, you can implement a game of dice for two players. And also the lottery, or roulette, for example. If you've ever played a game with procedural world generation, you now have an initial idea of how it works.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet