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 Dynamic Array
Java Program to Create a Random Graph Using Random Edge Generation
Tạo số và chuỗi ngẫu nhiên trong Java
Spring Security and OpenID Connect
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Implement ArrayDeque API
How to Read HTTP Headers in Spring REST Controllers
Guide to the Synchronized Keyword in Java
Spring’s RequestBody and ResponseBody Annotations
Getting the Size of an Iterable in Java
Java Program to Implement Best-First Search
LIKE Queries in Spring JPA Repositories
Java Program to Implement Levenshtein Distance Computing Algorithm
Converting Java Date to OffsetDateTime
Comparing Arrays in Java
Migrating from JUnit 4 to JUnit 5
The XOR Operator in Java
REST Pagination in Spring
Stack Memory and Heap Space in Java
Java Program to Implement Unrolled Linked List
Apache Commons Collections BidiMap
A Quick Guide to Spring Cloud Consul
Java Program to Perform Searching Using Self-Organizing Lists
Tổng quan về ngôn ngữ lập trình java
Guide to System.gc()
Period and Duration in Java
Java Program to Solve a Matching Problem for a Given Specific Case
New Features in Java 15
Java Program to Implement Circular Doubly Linked List
Java Program to Check the Connectivity of Graph Using DFS