Java Program to Generate Random Partition out of a Given Set of Numbers or Characters

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:

What is Thread-Safety and How to Achieve it?
Properties with Spring and Spring Boot
Guide to Spring @Autowired
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Spring Boot - Introduction
Phân biệt JVM, JRE, JDK
A Guide to Apache Commons Collections CollectionUtils
Java – Byte Array to Reader
Spring Cloud AWS – RDS
How to Replace Many if Statements in Java
Finding Max/Min of a List or Collection
Java Program to Find the Minimum value of Binary Search Tree
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Create a Random Graph Using Random Edge Generation
Allow user:password in URL
Convert char to String in Java
Date Time trong Java 8
String Joiner trong Java 8
Guide to Escaping Characters in Java RegExps
ExecutorService – Waiting for Threads to Finish
Tạo số và chuỗi ngẫu nhiên trong Java
Java Program to Solve a Matching Problem for a Given Specific Case
Spring’s RequestBody and ResponseBody Annotations
Collection trong java
Java Program to Solve the Fractional Knapsack Problem
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java Program to Solve any Linear Equation in One Variable
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Unrolled Linked List
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Implement the Vigenere Cypher
Java Program to Implement Aho-Corasick Algorithm for String Matching