Java Program to Generate Random Numbers in a Range

This is java program to generate the random numbers, in the range given by the user. Range could be any numbers of size integer supported by Java.

Here is the source code of the Java Program to Generate Randomized Sequence of Given Range of Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is the sample program to generate a randomized sequence of numbers
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Randomized_Sequence_Random_Numbers
  7. {
  8.     public static void main(String args[])
  9.     {
  10.         Random rand = new Random();
  11.         Scanner sc = new Scanner(System.in);
  12.         System.out.println("Enter the starting and ending of the sequence: ");
  13.         int start = sc.nextInt();
  14.         int end = sc.nextInt();
  15.  
  16.         for(int i=0; i<15; i++)
  17.         {
  18.             System.out.print(rand.nextInt(end-start+1)+start + ", ");
  19.         }
  20.         System.out.print("...");
  21.         sc.close();
  22.     }
  23. }

Output:

$ javac Randomizied_Sequence_Random_Numbers.java
$ java Randomizied_Sequence_Random_Numbers
Enter the starting and ending of the sequence: 
100
1000
490, 574, 179, 447, 723, 891, 589, 312, 667, 653, 375, 667, 990, 573, 399, ...

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.