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:
Disable DNS caching
How to Define a Spring Boot Filter?
Java – Random Long, Float, Integer and Double
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Implement Queue using Linked List
Guide to Apache Commons CircularFifoQueue
Chuyển đổi Array sang ArrayList và ngược lại
Merging Streams in Java
Jackson Ignore Properties on Marshalling
Consumer trong Java 8
Logging in Spring Boot
Java Program to Implement Trie
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Construct K-D Tree for 2 Dimensional Data
Simplify the DAO with Spring and Java Generics
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Solve any Linear Equations
Pagination and Sorting using Spring Data JPA
The Thread.join() Method in Java
Custom HTTP Header with the HttpClient
The Order of Tests in JUnit
So sánh ArrayList và Vector trong Java
Number Formatting in Java
Hướng dẫn Java Design Pattern – Command
Java Program to Implement HashMap API
Spring Webflux and CORS
Spring Boot - Apache Kafka
Java Program to Compute the Volume of a Tetrahedron Using Determinants
OAuth2 Remember Me with Refresh Token
Java Program to find the maximum subarray sum using Binary Search approach