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:
Introduction to PCollections
How to Remove the Last Character of a String?
Java Program to Decode a Message Encoded Using Playfair Cipher
Java Program to Optimize Wire Length in Electrical Circuit
Guide to Spring @Autowired
Java Program to Perform Complex Number Multiplication
How to Delay Code Execution in Java
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Sắp xếp trong Java 8
Introduction to Spring Cloud Netflix – Eureka
Spring Boot Configuration with Jasypt
A Guide to BitSet in Java
Java Program to Find the Mode in a Data Set
Hướng dẫn sử dụng Java Reflection
Java Program to Implement String Matching Using Vectors
Spring Boot - Zuul Proxy Server and Routing
Spring AMQP in Reactive Applications
Java Program to Represent Graph Using Incidence List
Comparing Arrays in Java
Java Program to Implement ConcurrentHashMap API
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Removing all Nulls from a List in Java
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Spring Security OAuth2 – Simple Token Revocation
Guide to @JsonFormat in Jackson
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Kết hợp Java Reflection và Java Annotations
Java Program to Perform Polygon Containment Test
Fixing 401s with CORS Preflights and Spring Security
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Implement Knight’s Tour Problem
Java Program to Check Cycle in a Graph using Graph traversal