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 Implement Ford–Fulkerson Algorithm
Java – Try with Resources
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Implement Sorted Singly Linked List
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java Program to Implement Pairing Heap
Java Program to Perform Uniform Binary Search
Java Program to Perform Sorting Using B-Tree
Guide to the Synchronized Keyword in Java
Multi Dimensional ArrayList in Java
LinkedList trong java
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Implement Borwein Algorithm
Spring Boot - Securing Web Applications
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Introduction to Spring Security Expressions
Spring Boot - Cloud Configuration Client
Properties with Spring and Spring Boot
Java Program to Find Strongly Connected Components in Graphs
Đồng bộ hóa các luồng trong Java
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Convert String to int or Integer in Java
Java Program to Implement Extended Euclid Algorithm
Java Program to Implement Graph Structured Stack
Convert String to Byte Array and Reverse in Java
Deploy a Spring Boot WAR into a Tomcat Server
The Guide to RestTemplate
Spring Boot - Google Cloud Platform
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
The Order of Tests in JUnit
Hướng dẫn Java Design Pattern – Transfer Object
Configure a Spring Boot Web Application