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 Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
A Guide to LinkedHashMap in Java
Java Program to Implement Extended Euclid Algorithm
Period and Duration in Java
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement Regular Falsi Algorithm
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Solve the Fractional Knapsack Problem
Java Program to Implement Double Ended Queue
Java Program to Implement Hash Tables with Quadratic Probing
String Processing with Apache Commons Lang 3
Java Program to Perform Search in a BST
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Implement SimpeBindings API
Query Entities by Dates and Times with Spring Data JPA
How to Convert List to Map in Java
Using Optional with Jackson
Tips for dealing with HTTP-related problems
How To Serialize and Deserialize Enums with Jackson
Introduction to Spring Cloud CLI
Spring Data MongoDB Transactions
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Perform Searching in a 2-Dimension K-D Tree
Lập trình đa luồng trong Java (Java Multi-threading)
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Guide to PriorityBlockingQueue in Java
Hashtable trong java
Java – Delete a File
Multi Dimensional ArrayList in Java
Spring Cloud Series – The Gateway Pattern
Hướng dẫn Java Design Pattern – Object Pool