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 Solve TSP Using Minimum Spanning Trees
A Guide to Java SynchronousQueue
Java Program to Describe the Representation of Graph using Adjacency List
Hướng dẫn sử dụng Java Reflection
Java InputStream to String
Guide to Mustache with Spring Boot
Spring Boot - Unit Test Cases
Spring Boot - Servlet Filter
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Solve the Fractional Knapsack Problem
Inheritance with Jackson
Java Program to Create the Prufer Code for a Tree
Split a String in Java
Jackson Unmarshalling JSON with Unknown Properties
The DAO with Spring and Hibernate
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Spring Security – security none, filters none, access permitAll
Mix plain text and HTML content in a mail
Query Entities by Dates and Times with Spring Data JPA
Java Program to Compute Determinant of a Matrix
Java Program to Implement Efficient O(log n) Fibonacci generator
Guide to ThreadLocalRandom in Java
Spring Boot - Enabling Swagger2
Java Program to Implement Find all Forward Edges in a Graph
Java Program to add two large numbers using Linked List
Introduction to Spring Cloud Netflix – Eureka
Introduction to Eclipse Collections
Spring Cloud – Tracing Services with Zipkin
Spring – Injecting Collections
XML-Based Injection in Spring
Convert XML to JSON Using Jackson