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:
Send email with authentication
A Guide to the ViewResolver in Spring MVC
Spring Boot - Eureka Server
Hướng dẫn Java Design Pattern – Object Pool
Java Program to Implement Min Heap
Comparing Long Values in Java
Java Program to implement Associate Array
Java Program to Implement Hopcroft Algorithm
Spring Boot - Cloud Configuration Server
Jackson vs Gson
Java Program to Show the Duality Transformation of Line and Point
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Guide to Mustache with Spring Boot
Hướng dẫn Java Design Pattern – Singleton
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Spring Boot Gradle Plugin
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Abstract class và Interface trong Java
The Java 8 Stream API Tutorial
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Introduction to the Functional Web Framework in Spring 5
Spring Boot - Sending Email
Java Program to Implement Queue using Linked List
Lập trình đa luồng với CompletableFuture trong Java 8
Guide to java.util.Formatter
Java Program to Represent Graph Using Adjacency List
Java Program to Convert a Decimal Number to Binary Number using Stacks
Using the Map.Entry Java Class
Serialization và Deserialization trong java
The Dining Philosophers Problem in Java
Java Map With Case-Insensitive Keys
Java Program to Implement Ternary Search Algorithm