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:
Jackson Ignore Properties on Marshalling
Java Program to Implement ConcurrentHashMap API
Assert an Exception is Thrown in JUnit 4 and 5
Java Collections Interview Questions
Một số nguyên tắc, định luật trong lập trình
ETL with Spring Cloud Data Flow
Java InputStream to String
How to Convert List to Map in Java
Deploy a Spring Boot App to Azure
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Optional as Return Type
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Spring Cloud – Securing Services
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Apache Camel with Spring Boot
Java InputStream to String
Generic Constructors in Java
Java Program to Implement Binary Tree
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Search for an Element in a Binary Search Tree
Spring Cloud AWS – EC2
Java Program to Implement Hash Tables with Quadratic Probing
Using Custom Banners in Spring Boot
Java 8 Stream API Analogies in Kotlin
An Intro to Spring Cloud Contract
Java Program to Implement Bucket Sort
Apache Commons Collections MapUtils
Handling URL Encoded Form Data in Spring REST
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Spring WebClient Filters
Introduction to Project Reactor Bus
Java Program to Find All Pairs Shortest Path