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
advertisement

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

If you find any mistake above, kindly email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.