-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpected-round-until-heads.cpp
77 lines (67 loc) · 1.83 KB
/
expected-round-until-heads.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <algorithm>
#include <functional>
#include <iostream>
#include <random>
#include <string>
using namespace std;
class ExpectedRoundSim {
public:
int coins;
const string HEADS = "HEAD", TAILS = "TAILS";
bool coinToss() {
static auto gen = std::bind(std::uniform_int_distribution<>(0, 1),
std::default_random_engine());
return gen();
}
string HeadOrTails() { return (coinToss() ? HEADS : TAILS); }
int tosses_until_head() {
int rounds = 0;
string toss;
do {
toss = HeadOrTails();
rounds += 1;
} while (toss != HEADS);
return rounds;
}
int tosses_until_tails() {
int rounds = 0;
string toss;
do {
toss = HeadOrTails();
rounds += 1;
} while (toss != TAILS);
return rounds;
}
void run_head_experiment(int n) {
// std::cout << "Tossing " << n << " coin at random on table" << std::endl;
int sum = 0;
for (int i = 0; i < n; i++) {
int rounds = tosses_until_head();
// std::cout << i << " th coin took " << rounds << std::endl;
sum += rounds;
}
std::cout << "Expected number of tosses until head per coin " << sum / n
<< std::endl;
}
void run_tail_experiment(int n) {
// std::cout << "Tossing " << n << " coin at random on table" << std::endl;
int sum = 0;
for (int i = 0; i < n; i++) {
int rounds = tosses_until_head();
// std::cout << i << " th coin took " << rounds << std::endl;
sum += rounds;
}
std::cout << "Expected number of tosses until tail per coin " << sum / n
<< std::endl;
}
void run_sim() {
run_head_experiment(1000000);
run_tail_experiment(1000000);
std::cout << std::endl;
}
};
int main(int argc, const char **argv) {
ExpectedRoundSim sim_1;
sim_1.run_sim();
return 0;
}