Java Program to Generate Random Number

In the previous article, we have discussed Java Program to Check If a Number is Positive or Negative

In this article we are going to see how we can generate random numbers in Java with examples.

Program to Generate Random Number

To generate random numbers we will be using the random function from the Java library. It takes integer value as argument and generates random numbers from 0 to the entered number.

Syntax: val= random.nextInt(upper_limit);

Let’s see different ways to generate random numbers.

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Approach :

  1. Enter/Declare a number and store it .
  2. We run a for loop to print 5 random numbers.
  3. We pass the entered number into the nextInt( ) function and run it.

Method-1: Java Program to Generate Random Number By User Input Value

import java.util.*;

public class RandomNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        // Creating an object from the random class
        Random rnd =  new Random();
        System.out.println("Random numbers generated between 0 - "+num+" : ");
        // Loop to print 5 random numbers
        for(int i = 1; i <= 5; i++)
        {
            System.out.print(rnd.nextInt(num)+", ");
        }
    }
}
Output:

Enter a number : 16
Random numbers generated between 0 - 16 : 
10, 14, 3, 15, 4,

Method-2: Java Program to Generate Random Number By User Defined Method

import java.util.*;

public class RandomNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        createNumber(num);
    }
    
     public static void createNumber(int num)
     {
        // Creating an object from the random class
        Random rnd =  new Random();
        System.out.println("Random numbers generated between 0 - "+num+" : ");
        // Loop to print 5 random numbers
        for(int i = 1; i <= 5; i++)
        {
            System.out.print(rnd.nextInt(num)+", ");
        }
     }
}
Output:

Enter a number : 16
Random numbers generated between 0 - 16 : 
9, 8, 8, 6, 14,

Method-3: Java Program to Generate Random Number By Using Static Value

import java.util.*;

public class RandomNumber
{
    public static void main(String args[])
    {
        //a number declared within which any randomnumbers will be generated
        int num = 10;

        // Creating an object from the random class
        Random rnd =  new Random();
        System.out.println("Random numbers generated between 0 - "+num+" : ");
        // Loop to print 5 random numbers
        for(int i = 1; i <= 5; i++)
        {
            System.out.print(rnd.nextInt(num)+", ");
        }
    }
}
Output:

Random numbers generated between 0 - 10 : 
0, 7, 1, 8, 2,

Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.

Related Java Programs: