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 Map With Case-Insensitive Keys
Java Program to Implement Fibonacci Heap
Java Program to implement Priority Queue
Tiêu chuẩn coding trong Java (Coding Standards)
Hướng dẫn Java Design Pattern – Builder
Hướng dẫn sử dụng lớp Console trong java
CharSequence vs. String in Java
Guide to CountDownLatch in Java
Java Program to Implement Queue
Java – String to Reader
Java Program to Implement Regular Falsi Algorithm
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Implement Heap Sort Using Library Functions
HttpClient Connection Management
Java Program to Implement PriorityQueue API
HashSet trong Java hoạt động như thế nào?
Collect a Java Stream to an Immutable Collection
Java Program to Implement the RSA Algorithm
Guide to the Volatile Keyword in Java
Creating a Web Application with Spring 5
How to Read HTTP Headers in Spring REST Controllers
Java Program to Use Dynamic Programming to Solve Approximate String Matching
New Features in Java 12
Apache Commons Collections BidiMap
Spring Boot - Enabling HTTPS
Debug a JavaMail Program
Java Program to Find Path Between Two Nodes in a Graph
Spring Cloud AWS – EC2
Java Program to Compute Cross Product of Two Vectors
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Implement Miller Rabin Primality Test Algorithm
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm