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 SynchronosQueue API
Java List UnsupportedOperationException
Converting a Stack Trace to a String in Java
Spring Boot Security Auto-Configuration
Java Program to Implement Johnson’s Algorithm
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Spring Cloud AWS – Messaging Support
Java Program to Solve any Linear Equation in One Variable
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Convert a Decimal Number to Binary Number using Stacks
Guide to the ConcurrentSkipListMap
JUnit 5 for Kotlin Developers
A Guide to TreeMap in Java
Merging Two Maps with Java 8
Java – Random Long, Float, Integer and Double
Removing all duplicates from a List in Java
Java Program to Implement Stack using Two Queues
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Map Serialization and Deserialization with Jackson
Optional trong Java 8
Spring Boot Application as a Service
Java Program to Implement Bucket Sort
wait() and notify() Methods in Java
Java Program to Implement Binary Tree
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Using Custom Banners in Spring Boot
Guide to the Java Queue Interface
Java Program to Implement Hash Tables Chaining with Binary Trees
Spring Boot - Application Properties
Introduction to Spliterator in Java
Java Program to Implement Levenshtein Distance Computing Algorithm