Java Program to Generate All Possible Combinations of a Given List of Numbers

This is a java program to generate sequence of N characters randomly.

Here is the source code of the Java Program to Generate a Sequence of N Characters for a Given Specific Case. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. // Specific case is that characters are generated randomly
  3. package com.sanfoundry.combinatorial;
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class SequenceOfNCharacters
  8. {
  9.     public static Integer randomInt(Integer low, Integer high)
  10.     {
  11.         return (int) (Math.floor(Math.random() * (high - low + 1)) + low);
  12.     }
  13.  
  14.     public static Character randomChar(String str)
  15.     {
  16.         return str.charAt(randomInt(0, str.length() - 1));
  17.     }
  18.  
  19.     public static String generateRandSeq(Integer length, String src)
  20.     {
  21.         String seq = "";
  22.         for (int i = 1; i <= length; i = i + 1)
  23.         {
  24.             seq += randomChar(src);
  25.         }
  26.         return seq;
  27.     }
  28.  
  29.     public static void main(String[] args)
  30.     {
  31.         String src = "abcdefghijklmnopqrstuvwxyz";
  32.         Scanner sc = new Scanner(System.in);
  33.         System.out.println("Enter the number of sequences to be generated: ");
  34.         int numberOfSequence = sc.nextInt();
  35.         System.out.println("Enter the length of each sequence: ");
  36.         int length = sc.nextInt();
  37.         for (int i = 0; i < numberOfSequence; i++)
  38.         {
  39.             System.out.println(generateRandSeq(length, src));
  40.         }
  41.         sc.close();
  42.     }
  43. }

Output:

$ javac SequenceOfNCharacters.java
$ java SequenceOfNCharacters
 
Enter the number of sequences to be generated: 
4
Enter the length of each sequence: 
5
qgpnt
kdxyr
ynhmf
wambi
 
Enter the number of sequences to be generated: 
3
Enter the length of each sequence: 
8
ilhddizq
evmpejxv
malvlhja

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.