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 – Create a File
Introduction to Spring Security Expressions
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Check Multiplicability of Two Matrices
Spring WebFlux Filters
Java Program to Implement Ford–Fulkerson Algorithm
Hướng dẫn Java Design Pattern – Observer
Send email with authentication
Java Deep Learning Essentials - Yusuke Sugomori
An Intro to Spring Cloud Zookeeper
Spring Boot - Cloud Configuration Client
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Derived Query Methods in Spring Data JPA Repositories
OAuth2 Remember Me with Refresh Token
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Lập trình mạng với java
Spring Boot - Scheduling
Jackson – Bidirectional Relationships
Spring Boot - Database Handling
Spring Boot - Batch Service
A Guide to HashSet in Java
An Intro to Spring Cloud Security
Hướng dẫn Java Design Pattern – Bridge
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement the Bin Packing Algorithm
Spring MVC Async vs Spring WebFlux
Simple Single Sign-On with Spring Security OAuth2
Setting the Java Version in Maven
An Example of Load Balancing with Zuul and Eureka
Java Program to Implement the Checksum Method for Small String Messages and Detect
Spring REST API with Protocol Buffers