Java Program to Generate a Random Subset by Coin Flipping

This is a java program to generate a random subset using coin flipping method. Coin is flipped, if is head(1), that element is in the subset else not in the subset.

Here is the source code of the Java Program to Generate a Random Subset by Coin Flipping. 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 generate a random subset using coin flipping 
import java.util.Random;
import java.util.Scanner;
 
public class Random_Subset_Coin_Flipping 
{
    static int coinFlip() 
    {
        Random random = new Random();
        return random.nextInt(2);
    }
 
    public static void main(String args[]) 
    {
        Random random = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements in the set: ");
        int N = sc.nextInt();
        int[] sequence = new int[N];
 
        for (int i = 0; i < N; i++)
            sequence[i] = Math.abs(random.nextInt(100));
 
        System.out.println("The elements in the set : ");
        for (int i = 0; i < N; i++)
            System.out.print(sequence[i] + " ");
 
        System.out.print("\nThe random subset is: \n{ ");
        for (int i = 0; i < N; i++)
            if (coinFlip() == 1)
                System.out.print(sequence[i] + " ");
        System.out.println("}");
        sc.close();
    }
}

Output:

$ javac Random_Subset_Coin_Flipping.java
$ java Random_Subset_Coin_Flipping
 
Enter the number of elements in the set: 
10
The elements in the set : 
1 44 88 58 8 62 94 59 38 33 
The random subset is: 
{ 1 44 8 33 }
 
Enter the number of elements in the set: 
3
The elements in the set : 
0 42 91 
The random subset is: 
{ 42 91 }

Related posts:

Guide to Spring @Autowired
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Find Basis and Dimension of a Matrix
Hướng dẫn Java Design Pattern – Transfer Object
Check If a File or Directory Exists in Java
Java Program to Implement SimpeBindings API
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Lớp TreeMap trong Java
Set Interface trong Java
Spring @RequestParam Annotation
Java Program to Find the Longest Path in a DAG
New Features in Java 9
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement Sieve Of Sundaram
Spring Data MongoDB Transactions
Guide to Guava Table
Java Program to Find the Connected Components of an UnDirected Graph
New Features in Java 8
How to Round a Number to N Decimal Places in Java
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Using a Spring Cloud App Starter
Java Program to Implement Bit Array
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to Implement Johnson’s Algorithm
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement JobStateReasons API
Working with Network Interfaces in Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java 8 Predicate Chain
How to Read HTTP Headers in Spring REST Controllers