Java Program to Generate Random Numbers Between Given Ranges

You are currently viewing Java Program to Generate Random Numbers Between Given Ranges

Java program to generate random numbers between given ranges

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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply