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:

Converting Between Byte Arrays and Hexadecimal Strings in Java
Receive email by java client
The HttpMediaTypeNotAcceptableException in Spring MVC
Function trong Java 8
Java Program to Implement JobStateReasons API
Java Program to Implement the Checksum Method for Small String Messages and Detect
An Introduction to Java.util.Hashtable Class
Java Program to Implement Hash Tables with Quadratic Probing
REST Web service: Basic Authentication trong Jersey 2.x
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Guide to Spring Cloud Kubernetes
Luồng Daemon (Daemon Thread) trong Java
Using Java Assertions
Java Program to Represent Linear Equations in Matrix Form
Hashtable trong java
Hướng dẫn Java Design Pattern – Bridge
Java String Conversions
Java – String to Reader
The Registration Process With Spring Security
Java Program to Implement Aho-Corasick Algorithm for String Matching
Generate Spring Boot REST Client with Swagger
Java Program to Implement CopyOnWriteArrayList API
HashMap trong Java hoạt động như thế nào?
How To Serialize and Deserialize Enums with Jackson
A Quick Guide to Spring MVC Matrix Variables
Giới thiệu Google Guice – Dependency injection (DI) framework
Guide to java.util.Formatter
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Implement Threaded Binary Tree
Validations for Enum Types
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree