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 8 – Powerful Comparison with Lambdas
Java Program to Implement Attribute API
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Functional Interfaces in Java 8
Biến trong java
Object cloning trong java
SOAP Web service: Authentication trong JAX-WS
JUnit5 @RunWith
Arrays.asList vs new ArrayList(Arrays.asList())
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Flattening Nested Collections in Java
Java Program to Implement Hash Trie
Static Content in Spring WebFlux
Spring WebClient Requests with Parameters
Spring Cloud Series – The Gateway Pattern
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Spring Boot Tutorial – Bootstrap a Simple Application
Spring Boot Application as a Service
Spring Security Custom AuthenticationFailureHandler
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Setting the Java Version in Maven
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Remove the First Element from a List
Java Program to Implement the String Search Algorithm for Short Text Sizes
The Modulo Operator in Java
Exploring the Spring 5 WebFlux URL Matching
Java Program to Perform Searching Based on Locality of Reference
JUnit 5 @Test Annotation
Java Program to Implement CountMinSketch
Exploring the Spring Boot TestRestTemplate
Guide to @ConfigurationProperties in Spring Boot
Java Program to Implement Shoelace Algorithm