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:
A Quick Guide to Spring MVC Matrix Variables
Tính trừu tượng (Abstraction) trong Java
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to add two large numbers using Linked List
Java Program to Implement Stack API
Java Program to Implement Sparse Matrix
A Quick Guide to Spring Cloud Consul
Java Program to Encode a Message Using Playfair Cipher
Java Program to Implement Vector API
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
How to Get a Name of a Method Being Executed?
Custom HTTP Header with the HttpClient
Mệnh đề Switch-case trong java
Java Program to Implement Self Balancing Binary Search Tree
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Jackson Unmarshalling JSON with Unknown Properties
Spring Cloud – Securing Services
CyclicBarrier in Java
Java – Convert File to InputStream
ExecutorService – Waiting for Threads to Finish
Setting the Java Version in Maven
List Interface trong Java
Java Program to Represent Graph Using Linked List
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Guide To CompletableFuture
Copy a List to Another List in Java
Java Program to Implement Fermat Factorization Algorithm
Introduction to Java 8 Streams
Introduction to Spring MVC HandlerInterceptor
Quick Guide to Spring Bean Scopes
Ép kiểu trong Java (Type casting)