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 Program to Construct an Expression Tree for an Prefix Expression
Hướng dẫn Java Design Pattern – Mediator
Java Program to Implement Attribute API
Java Program to Implement Pairing Heap
Working with Kotlin and JPA
Truyền giá trị và tham chiếu trong java
So sánh Array và ArrayList trong Java
Các kiểu dữ liệu trong java
Java Program to Implement Skew Heap
Java Program to Implement a Binary Search Tree using Linked Lists
Easy Ways to Write a Java InputStream to an OutputStream
Hướng dẫn sử dụng Java Annotation
Apache Commons Collections SetUtils
Entity To DTO Conversion for a Spring REST API
Java Program to Represent Graph Using Adjacency List
Consumer trong Java 8
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement Floyd-Warshall Algorithm
Programmatic Transaction Management in Spring
OAuth2.0 and Dynamic Client Registration
Bootstrap a Web Application with Spring 5
Partition a List in Java
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Getting a File’s Mime Type in Java
The Registration API becomes RESTful
Removing Elements from Java Collections
Comparing Long Values in Java
Java Program to Implement Rolling Hash
Guide to Java 8’s Collectors