
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Random Subset by Coin Flipping in C++
The subsets of a set refers to all the possible combinations of choosing elements from the set. For example, if we have a set {1, 2}, the subsets are {}, {1}, {2}, and {1, 2}. In this article, we will learn how to generate a random subset for a set using coin flipping technique in C++.
// Set of elements int arr[] = {1, 2, 3}; // Subsets of the above set {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
Coin Flipping Technique
The coin flipping technique is a simple way to generate a random subset of a set. The idea is to iterate through each element of the set and randomly decide whether to include the element in the subset or not. This is similar to flipping a coin for each element to decide if it should be included or not. That is why this method is called as coin flipping technique.
// Consider a set of elements int arr[] = {1, 2, 3, 4, 5}; // Randomly decide whether to include each element in the subset arr = { 1, // Include 2, // Exclude 3, // Include 4, // Exclude 5 // Include }; Random Subset = {1, 3, 5}
Steps to Generate a Random Subset of a Set
- Initialize an set of elements (you can use an array, vector, or a set for this )
- Use a loop to iterate through each element of the set
- For each element, generate a binary random number (0 or 1)
- If the random number is 1, include the element in the subset
- If the random number is 0, don't include the element to the subset
- Continue this process for all elements in the set
C++ Code to Generate a Random Subset of a Set
In the code below, we defined a function generateRandomSubset that takes a int vector as input and generates a random subset for that vector set. Here we used the rand() function to generate a binary random number.
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> using namespace std; void generateRandomSubset(const vector<int>& set) { vector<int> subset; srand(time(0)); // Seed for randomness cout << "Original Set: { "; for (int num : set) { cout << num << " "; } cout << "}" << endl; cout << "Random Subset: { "; for (int i = 0; i < set.size(); ++i) { int coin = rand() % 2; // 0 or 1 if (coin == 1) { subset.push_back(set[i]); cout << set[i] << " "; } } cout << "}" << endl; } int main() { vector<int> set = {1, 2, 3, 4, 5}; generateRandomSubset(set); return 0; }
The output of above code will be:
Original Set: { 1 2 3 4 5 } Random Subset: { 1 3 5 }
Note: The output may vary each time you run the program due to the randomness happening while selecting the subset.
Time and Space Complexity
Time Complexity: The time complexity of this algorithm is O(n), where n is the number of elements in the set. This is because we iterate through each element of the set exactly once to decide whether to include it in the subset.
Space Complexity: The space complexity is also O(n), considering the worst case where all elements are included in the subset.