Java Program to Generate a Sequence of N Characters for a Given Specific Case

«
»
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:

advertisement
$ 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 Reference Books in Java Programming, Data Structures and Algorithms.

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter