This is a java program to generate a random partitioning of a set of characters or numbers in to two sets. Randomly generate an index less than the total number of elements in the set.
Here is the source code of the Java Program to Generate Random Partition out of a Given Set of Numbers or Characters. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to perform partitioning at random index and generate two sets for given set of numbers or characters
import java.util.Random;
import java.util.Scanner;
public class Random_Partition
{
public static void main(String args[])
{
Random random = new Random();
Scanner sc = new Scanner(System.in);
int noc = random.nextInt(2);
// if noc is equal to 1 generate numbers
if (noc == 1)
{
int N = 10;
int[] sequence = new int[N];
System.out.print("The Original set of numbers are:\n ");
for (int i = 0; i < N; i++)
{
sequence[i] = Math.abs(random.nextInt(100));
System.out.print(sequence[i] + " ");
}
int partition_index = random.nextInt(11);
System.out.println("\nThe two sequemces are: ");
System.out.print("{ ");
for (int i = 0; i < N; i++)
{
if (i == partition_index)
System.out.print(" } and { ");
System.out.print(sequence[i] + " ");
}
System.out.print("}");
System.out
.println("\nPartitioning around index " + partition_index);
}
// else generate characters
else
{
int N = 10;
char[] sequence = new char[N];
System.out.print("The Original set of characters are:\n ");
for (int i = 0; i < N; i++)
{
sequence[i] = (char) Math.abs(random.nextInt(123 - 97) + 97);
System.out.print(sequence[i] + " ");
}
int partition_index = random.nextInt(11);
System.out.println("\nThe two sequences are: ");
System.out.print("{ ");
for (int i = 0; i < N; i++)
{
if (i == partition_index)
System.out.print(" } and { ");
System.out.print(sequence[i] + " ");
}
System.out.print("}");
System.out
.println("\nPartitioning around index " + partition_index);
}
sc.close();
}
}
Output:
$ javac Random_Partition.java
$ java Random_Partition
The Original set of numbers are:
70 13 10 36 78 98 18 64 60 84
The two sequences are:
{ 70 13 10 36 78 98 18 64 } and { 60 84 }
Partitioning around index 8
The Original set of characters are:
n p r e m z y o x p
The two sequences are:
{ n p r e m z } and { y o x p }
Partitioning around index 6
Related posts:
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
New Features in Java 9
Spring Cloud AWS – RDS
Java – Write a Reader to File
How to Manually Authenticate User with Spring Security
Java Program to Implement Find all Forward Edges in a Graph
LIKE Queries in Spring JPA Repositories
Hướng dẫn Java Design Pattern – Template Method
Toán tử trong java
Returning Custom Status Codes from Spring Controllers
Java Program to Implement Hamiltonian Cycle Algorithm
Spring Boot - Quick Start
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
DynamoDB in a Spring Boot Application Using Spring Data
The Registration API becomes RESTful
Java Program to Implement Sieve Of Sundaram
Java Program to Implement Rolling Hash
StringBuilder vs StringBuffer in Java
Java Convenience Factory Methods for Collections
What is Thread-Safety and How to Achieve it?
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
REST Pagination in Spring
Giới thiệu Design Patterns
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Guide to BufferedReader
Java Program to Implement Affine Cipher
Java Program to Implement Sparse Array
Spring Boot - Database Handling
Java Program to Implement Brent Cycle Algorithm
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java Program to Implement Selection Sort
Java Program to Implement Triply Linked List