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 Find the Peak Element of an Array O(n) time (Naive Method)
How to Read a Large File Efficiently with Java
Java Program to Implement VList
Java Program to Implement DelayQueue API
Guava Collections Cookbook
Recommended Package Structure of a Spring Boot Project
Converting Java Date to OffsetDateTime
Java Program to Implement Fermat Primality Test Algorithm
How to Change the Default Port in Spring Boot
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Function trong Java 8
Java Program to Evaluate an Expression using Stacks
Spring Security with Maven
Spring Data MongoDB – Indexes, Annotations and Converters
Spring Boot - File Handling
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java – Write an InputStream to a File
Comparing Dates in Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
A Guide to the ViewResolver in Spring MVC
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
A Guide to Java 9 Modularity
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Check If a String Is Numeric in Java
Lấy ngày giờ hiện tại trong Java
New Stream Collectors in Java 9
Comparing Two HashMaps in Java
Toán tử trong java
Getting Started with Custom Deserialization in Jackson
Java Program to Implement Variable length array
More Jackson Annotations