Generate Random Integer Numbers in Java

In order to generate Random Integer Numbers in Java, we use the nextInt() method of the java.util.Random class. This returns the next random integer value from this random number generator sequence.

Declaration − The java.util.Random.nextInt() method is declared as follows −

public int nextInt()

Let us see a program to generate random integer numbers in Java −


 Live Demo

import java.util.Random;
public class Example {
   public static void main(String[] args) {
      Random rd = new Random(); // creating Random object



Note - The output might vary on Online Compilers.

Updated on: 25-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started