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:
Checking for Empty or Blank Strings in Java
Java Program to Implement Brent Cycle Algorithm
Introduction to Spring MVC HandlerInterceptor
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Compute Determinant of a Matrix
Converting a Stack Trace to a String in Java
Java Program to Implement Adjacency List
Java Program to Implement Graph Structured Stack
Spring Data JPA Delete and Relationships
Remove All Occurrences of a Specific Value from a List
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Java Program to Implement Gabow Algorithm
Serve Static Resources with Spring
Returning Custom Status Codes from Spring Controllers
Java Program to Implement Fibonacci Heap
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Check whether Graph is a Bipartite using BFS
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Implement Pollard Rho Algorithm
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement ArrayList API
An Intro to Spring Cloud Vault
Java Program to Check if a Matrix is Invertible
The Guide to RestTemplate
Spring Boot - Code Structure
Allow user:password in URL
File Upload with Spring MVC
Creating Docker Images with Spring Boot
Java Program to Describe the Representation of Graph using Adjacency List
Spring AMQP in Reactive Applications
Spring Boot - Google Cloud Platform