Java Program to Generate Random Numbers Between Given Ranges

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);
	}  
}
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

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

Share on:

Leave a Comment