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 EnumMap API
Java Collections Interview Questions
Guide to Spring @Autowired
Spring Boot - Actuator
Java Program to Implement Hash Tables Chaining with Binary Trees
Java 8 StringJoiner
Working with Network Interfaces in Java
Hướng dẫn Java Design Pattern – MVC
Java InputStream to String
A Guide To UDP In Java
Lớp Collections trong Java (Collections Utility Class)
Ignore Null Fields with Jackson
Spring NoSuchBeanDefinitionException
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Implement Adjacency List
Login For a Spring Web App – Error Handling and Localization
Giới thiệu về Stream API trong Java 8
Spring MVC Content Negotiation
Comparing Two HashMaps in Java
Quick Guide to java.lang.System
Spring Boot Gradle Plugin
Java Program to implement Array Deque
A Guide to ConcurrentMap
Java Program to Implement Sorted Array
Hướng dẫn Java Design Pattern – Iterator
Guide to Character Encoding
Java 14 Record Keyword
The “final” Keyword in Java
The Difference Between Collection.stream().forEach() and Collection.forEach()
Spring Boot - Quick Start
Spring Cloud AWS – RDS
Toán tử instanceof trong java