How to resolve the algorithm Unbias a random generator step by step in the C++ programming language
How to resolve the algorithm Unbias a random generator step by step in the C++ programming language
Table of Contents
Problem Statement
The actual unbiasing should be done by generating two numbers at a time from randN and only returning a 1 or 0 if they are different. As long as you always return the first number or always return the second number, the probabilities discussed above should take over the biased probability of randN. This task is an implementation of Von Neumann debiasing, first described in a 1951 paper.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Unbias a random generator step by step in the C++ programming language
The provided C++ code simulates two types of coin flips: biased and unbiased. A biased coin flip gives a result of 1
with a higher probability, while an unbiased coin flip gives a result of 0
or 1
with equal probability.
Here's a breakdown of the code:
-
The code includes the
<iostream>
and<random>
header files. -
A global
std::default_random_engine
namedgenerator
is initialized. This is used for generating random numbers. -
biased(int n)
function:- It takes an integer
n
as an argument. - It uses a
std::uniform_int_distribution
to generate a random integer between 1 andn
. - It returns
true
if the generated integer is 1, which represents a biased coin flip towards1
.
- It takes an integer
-
unbiased(int n)
function:- It takes an integer
n
as an argument. - It uses two
biased
coin flips to simulate an unbiased coin flip. - It first performs two biased coin flips and checks if the results are the same.
- If the results are different, it returns the result of the first flip.
- If the results are the same, it performs another biased coin flip and repeats the process until it gets different results.
- This method effectively simulates an unbiased coin flip because the probability of getting
0
or1
is equal.
- It takes an integer
-
In the
main
function:- A loop iterates from
n = 3
ton = 6
. - For each
n
, it simulates 100,000 biased and unbiased coin flips. - It counts the number of times
0
or1
appears in both biased and unbiased flips. - Finally, it prints out the results for each
n
, showing the percentages of0
and1
appearing in both biased and unbiased flips.
- A loop iterates from
The output of the program shows that the biased coin flip favors 1
, as expected, while the unbiased coin flip has close to 50% probability for both 0
and 1
.
Source code in the cpp programming language
#include <iostream>
#include <random>
std::default_random_engine generator;
bool biased(int n) {
std::uniform_int_distribution<int> distribution(1, n);
return distribution(generator) == 1;
}
bool unbiased(int n) {
bool flip1, flip2;
/* Flip twice, and check if the values are the same.
* If so, flip again. Otherwise, return the value of the first flip. */
do {
flip1 = biased(n);
flip2 = biased(n);
} while (flip1 == flip2);
return flip1;
}
int main() {
for (size_t n = 3; n <= 6; n++) {
int biasedZero = 0;
int biasedOne = 0;
int unbiasedZero = 0;
int unbiasedOne = 0;
for (size_t i = 0; i < 100000; i++) {
if (biased(n)) {
biasedOne++;
} else {
biasedZero++;
}
if (unbiased(n)) {
unbiasedOne++;
} else {
unbiasedZero++;
}
}
std::cout << "(N = " << n << ")\n";
std::cout << "Biased:\n";
std::cout << " 0 = " << biasedZero << "; " << biasedZero / 1000.0 << "%\n";
std::cout << " 1 = " << biasedOne << "; " << biasedOne / 1000.0 << "%\n";
std::cout << "Unbiased:\n";
std::cout << " 0 = " << unbiasedZero << "; " << unbiasedZero / 1000.0 << "%\n";
std::cout << " 1 = " << unbiasedOne << "; " << unbiasedOne / 1000.0 << "%\n";
std::cout << '\n';
}
return 0;
}
You may also check:How to resolve the algorithm Middle three digits step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Two's complement step by step in the Phix programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Java programming language
You may also check:How to resolve the algorithm Singleton step by step in the Latitude programming language
You may also check:How to resolve the algorithm Range extraction step by step in the Fortran programming language