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:
Spring Boot - Scheduling
Supplier trong Java 8
Convert Character Array to String in Java
Java Program to Perform Polygon Containment Test
Java Program to Implement Network Flow Problem
Quick Guide to @RestClientTest in Spring Boot
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java Program to Implement Stack using Linked List
Spring Boot - Flyway Database
Java Program to Create the Prufer Code for a Tree
Optional trong Java 8
Login For a Spring Web App – Error Handling and Localization
Java Streams vs Vavr Streams
Creating a Web Application with Spring 5
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Một số từ khóa trong Java
Introduction to Spring Cloud Stream
Introduction to Java Serialization
Java Multi-line String
Guide to UUID in Java
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Hash Tables
Jackson JSON Views
Spring Boot - Thymeleaf
How to Manually Authenticate User with Spring Security
The Registration Process With Spring Security
Copy a List to Another List in Java
Java Program to Implement Cartesian Tree
Working with Kotlin and JPA
Java – Combine Multiple Collections
Guide to java.util.concurrent.Locks