Java Program to Generate Random Partition from Given Set

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.

  1. //This is a java program to perform partitioning at random index and generate two sets for given set of numbers or characters
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Random_Partition 
  6. {
  7.     public static void main(String args[]) 
  8.     {
  9.         Random random = new Random();
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         int noc = random.nextInt(2);
  13.         // if noc is equal to 1 generate numbers
  14.         if (noc == 1) 
  15.         {
  16.             int N = 10;
  17.             int[] sequence = new int[N];
  18.             System.out.print("The Original set of numbers are:\n  ");
  19.             for (int i = 0; i < N; i++) 
  20.             {
  21.                 sequence[i] = Math.abs(random.nextInt(100));
  22.                 System.out.print(sequence[i] + " ");
  23.             }
  24.  
  25.             int partition_index = random.nextInt(11);
  26.             System.out.println("\nThe two sequemces are: ");
  27.             System.out.print("{ ");
  28.             for (int i = 0; i < N; i++) 
  29.             {
  30.                 if (i == partition_index)
  31.                     System.out.print(" } and { ");
  32.                 System.out.print(sequence[i] + " ");
  33.             }
  34.             System.out.print("}");
  35.             System.out
  36.                     .println("\nPartitioning around index " + partition_index);
  37.  
  38.         }
  39.         // else generate characters
  40.         else
  41.         {
  42.             int N = 10;
  43.             char[] sequence = new char[N];
  44.             System.out.print("The Original set of characters are:\n  ");
  45.             for (int i = 0; i < N; i++) 
  46.             {
  47.                 sequence[i] = (char) Math.abs(random.nextInt(123 - 97) + 97);
  48.                 System.out.print(sequence[i] + " ");
  49.             }
  50.  
  51.             int partition_index = random.nextInt(11);
  52.             System.out.println("\nThe two sequences are: ");
  53.             System.out.print("{ ");
  54.             for (int i = 0; i < N; i++) 
  55.             {
  56.                 if (i == partition_index)
  57.                     System.out.print(" } and { ");
  58.                 System.out.print(sequence[i] + " ");
  59.             }
  60.             System.out.print("}");
  61.             System.out
  62.                     .println("\nPartitioning around index " + partition_index);
  63.  
  64.         }
  65.         sc.close();
  66.     }
  67. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.