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 Borwein Algorithm
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Implement Iterative Deepening
The Order of Tests in JUnit
Spring Security with Maven
Hướng dẫn sử dụng Java Annotation
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Spring Boot Configuration with Jasypt
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java Program to Implement Sparse Matrix
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java Program to Implement Binomial Heap
Spring Boot - Cloud Configuration Client
Java Program to Describe the Representation of Graph using Incidence List
Migrating from JUnit 4 to JUnit 5
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
The Spring @Controller and @RestController Annotations
How to Break from Java Stream forEach
Java Program to Find Strongly Connected Components in Graphs
Validate email address exists or not by Java Code
Hướng dẫn Java Design Pattern – Service Locator
Database Migrations with Flyway
Convert String to Byte Array and Reverse in Java
Hướng dẫn Java Design Pattern – Singleton
Spring Cloud Bus
Java Program to Implement D-ary-Heap
Java Program to Implement Gauss Seidel Method
@DynamicUpdate with Spring Data JPA
Java Program to Implement Solovay Strassen Primality Test Algorithm
JUnit5 @RunWith
Biến trong java