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.

// Specific case is that characters are generated randomly
package com.sanfoundry.combinatorial;
 
import java.util.Scanner;
 
public class SequenceOfNCharacters
{
    public static Integer randomInt(Integer low, Integer high)
    {
        return (int) (Math.floor(Math.random() * (high - low + 1)) + low);
    }
 
    public static Character randomChar(String str)
    {
        return str.charAt(randomInt(0, str.length() - 1));
    }
 
    public static String generateRandSeq(Integer length, String src)
    {
        String seq = "";
        for (int i = 1; i <= length; i = i + 1)
        {
            seq += randomChar(src);
        }
        return seq;
    }
 
    public static void main(String[] args)
    {
        String src = "abcdefghijklmnopqrstuvwxyz";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of sequences to be generated: ");
        int numberOfSequence = sc.nextInt();
        System.out.println("Enter the length of each sequence: ");
        int length = sc.nextInt();
        for (int i = 0; i < numberOfSequence; i++)
        {
            System.out.println(generateRandSeq(length, src));
        }
        sc.close();
    }
}

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

Related posts:

Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Introduction to Spring MVC HandlerInterceptor
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Custom HTTP Header with the HttpClient
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Generate All Possible Combinations of a Given List of Numbers
How to Read a Large File Efficiently with Java
Object cloning trong java
Number Formatting in Java
Java Program to Implement Caesar Cypher
Setting a Request Timeout for a Spring REST API
Instance Profile Credentials using Spring Cloud
Spring Cloud AWS – EC2
Java Program to Implement Find all Back Edges in a Graph
Lớp lồng nhau trong java (Java inner class)
A Guide to JUnit 5 Extensions
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Find the GCD and LCM of two Numbers
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Implement Kosaraju Algorithm
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Implement Quick sort
Java IO vs NIO
Create Java Applet to Simulate Any Sorting Technique
Spring Boot: Customize the Jackson ObjectMapper
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
A Guide to Java SynchronousQueue
Use Liquibase to Safely Evolve Your Database Schema
Java 8 – Powerful Comparison with Lambdas
CharSequence vs. String in Java