-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday66_biased_toss.cpp
More file actions
61 lines (49 loc) · 1.17 KB
/
Copy pathday66_biased_toss.cpp
File metadata and controls
61 lines (49 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by Square.
Assume you have access to a function toss_biased() which returns 0 or 1 with a probability that's not 50-50 (but also not 0-100 or 100-0). You do not know the bias of the coin.
Write a function to simulate an unbiased coin toss.
*/
#include <gtest/gtest.h>
using namespace std;
bool toss_biased()
{
return rand() % 10 < 6; // 0..5 = true, 6..9 = false (ie 6/10 true)
}
/**
* Idea:
* Let p be our function and pb the biased toss
* p(1) = 1/2 = p(0)
* Also, pb(1) = x and pb(0) = 1 - x
*
* pb(0, 0) = (1-x)²
* pb(1, 0) = x(1-x)
* pb(0, 1) = (1-x)x
* pb(1, 1) = x²
*
* So pb(1, 0) and pb(0, 1) have the same probability of occuring.
*/
bool toss()
{
while (true)
{
bool a = toss_biased();
bool b = toss_biased();
if (a ^ b)
return a && !b;
}
}
TEST(TOSS, toss)
{
srand(1);
int len = 10000;
int c = 0;
for (int i = 0; i < len; i++)
c += toss();
cout << "Result " << c * 1.0 / len << endl;
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}