×

Java Programs

Java Practice

Java program to generate random numbers between given ranges

Here, we are implementing a java program that will generate and print random numbers between given ranges?
Submitted by IncludeHelp, on January 02, 2018

Given ranges of the numbers and we have to generate and print the random number using java program.

    Input:
    Enter starting range: 5
    Enter final range: 50

    Output:
    Random number between given range: 18

Program to generate and print random number in Java

import java.util.Scanner;

public class GenerateRandomIntegers
{
	public static void main( String args[] )
	{ 
		// create object here.
		Scanner sc = new Scanner( System.in ); 

		// enter the range between which we have to print random numbers.
		System.out.print("Enter starting range : "); 

		int rsnum = sc.nextInt(); 

		System.out.print("Enter final range : "); 

		int renum = sc.nextInt(); 

		int random_num = rsnum + (int)(Math.random() * ((renum - rsnum) + 1));
		System.out.println("Random number between given range : " +random_num);
	}  
}

Output

First run:
Enter starting range : 5
Enter final range : 50
Random number between given range : 18

Second run:
Enter starting range : 1
Enter final range : 10
Random number between given range : 5

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.