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 - Service Components
Optional trong Java 8
Java Program to Implement Hamiltonian Cycle Algorithm
Custom HTTP Header with the HttpClient
Spring Boot - Unit Test Cases
Spring Cloud AWS – EC2
Phân biệt JVM, JRE, JDK
Java Program to Implement Knapsack Algorithm
Java Program to Implement LinkedBlockingDeque API
Tính đóng gói (Encapsulation) trong java
Java Program to Implement Floyd Cycle Algorithm
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Weak References in Java
Apache Commons Collections BidiMap
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Implement Randomized Binary Search Tree
Lập trình mạng với java
Java Program to find the number of occurrences of a given number using Binary Search approach
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Warshall Algorithm
Generating Random Dates in Java
Java Program to Implement LinkedHashMap API
Java Program to Implement Double Ended Queue
How to Change the Default Port in Spring Boot
Java Program to Generate Random Numbers Using Multiply with Carry Method
Guide to Spring 5 WebFlux
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Perform Uniform Binary Search
Java Program to Solve the Fractional Knapsack Problem
Java Program to Implement Graph Coloring Algorithm