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:

Java Program to Implement Max Heap
Spring @RequestParam Annotation
Inheritance with Jackson
Java Program to Implement Repeated Squaring Algorithm
A Guide to Concurrent Queues in Java
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Spring REST API + OAuth2 + Angular
Java Program to Implement ArrayDeque API
Guide to the Volatile Keyword in Java
Java Web Services – JAX-WS – SOAP
Introduction to Spring Data JPA
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
List Interface trong Java
Default Password Encoder in Spring Security 5
Java Program to Implement Regular Falsi Algorithm
Getting Started with Stream Processing with Spring Cloud Data Flow
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Implement Self Balancing Binary Search Tree
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement the String Search Algorithm for Short Text Sizes
Converting Between a List and a Set in Java
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Implement Naor-Reingold Pseudo Random Function
Introduction to Java 8 Streams
Hướng dẫn Java Design Pattern – Factory Method
Spring Boot - Scheduling
New Stream Collectors in Java 9
Java 8 Predicate Chain
HttpClient 4 – Follow Redirects for POST