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:
Spring Boot - Introduction
Using Custom Banners in Spring Boot
Spring Security Form Login
Use Liquibase to Safely Evolve Your Database Schema
Beans and Dependency Injection
Java Program to Implement Stein GCD Algorithm
Comparing Arrays in Java
Debugging Reactive Streams in Java
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Notify User of Login From New Device or Location
Extra Login Fields with Spring Security
Java Program to Solve any Linear Equations
Java Program to Implement Sieve Of Sundaram
A Guide to HashSet in Java
Guide to the Synchronized Keyword in Java
Tổng quan về ngôn ngữ lập trình java
Java Program to Perform Uniform Binary Search
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
A Guide to TreeSet in Java
Adding Shutdown Hooks for JVM Applications
So sánh ArrayList và LinkedList trong Java
Hashing a Password in Java
Netflix Archaius with Various Database Configurations
Java Program to Implement Quick Sort Using Randomization
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Represent Linear Equations in Matrix Form
Reversing a Linked List in Java
Java Stream Filter with Lambda Expression
Java Program to Implement Hopcroft Algorithm
Java Program to Implement Gabow Algorithm
Java – Combine Multiple Collections
Java Program to Perform Partial Key Search in a K-D Tree