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:
Control Structures in Java
Java Program to Represent Graph Using Incidence List
Collection trong java
Java Program to Implement Attribute API
Java Program to Implement D-ary-Heap
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Check if it is a Sparse Matrix
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Implement Trie
Setting the Java Version in Maven
HTTP Authentification and CGI/Servlet
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Testing in Spring Boot
Spring Boot Configuration with Jasypt
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Implement Gabow Algorithm
Java Program to Implement Counting Sort
Java Program to Check whether Graph is a Bipartite using DFS
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Tổng quan về ngôn ngữ lập trình java
The Difference Between map() and flatMap()
Spring Boot - Code Structure
Why String is Immutable in Java?
Overview of Spring Boot Dev Tools
Java Program to Implement Treap
Check if a String is a Palindrome in Java
Java 8 Predicate Chain
Biến trong java
HashSet trong java
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Lớp Arrarys trong Java (Arrays Utility Class)