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 Implement Vector API
Java Program to Perform Search in a BST
A Comparison Between Spring and Spring Boot
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java – Random Long, Float, Integer and Double
Lớp lồng nhau trong java (Java inner class)
Java Program to Perform Complex Number Multiplication
The Spring @Controller and @RestController Annotations
Guide to java.util.concurrent.BlockingQueue
A Guide to Java SynchronousQueue
Spring Boot - CORS Support
Phân biệt JVM, JRE, JDK
Java Program to Solve any Linear Equations
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Implement Ternary Tree
Stack Memory and Heap Space in Java
Jackson Exceptions – Problems and Solutions
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Implement Pairing Heap
The Registration API becomes RESTful
Java – Reader to Byte Array
Using JWT with Spring Security OAuth (legacy stack)
XML-Based Injection in Spring
Java Program to Implement TreeSet API
New Features in Java 13
Ép kiểu trong Java (Type casting)
A Guide to Java 9 Modularity
Guide to java.util.concurrent.Locks
Cài đặt và sử dụng Swagger UI
Introduction to Eclipse Collections
Weak References in Java
Spring Security 5 – OAuth2 Login