-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcoinFlip.cpp
More file actions
38 lines (33 loc) · 791 Bytes
/
coinFlip.cpp
File metadata and controls
38 lines (33 loc) · 791 Bytes
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
/*
* C++11 Program to simulate a coin flip using Bernoulli's distribution
*/
#include<iostream>
#include<sstream>
#include<random>
#include<vector>
int main()
{
int n;
std::random_device rd;
std::mt19937 mt(rd());
std::bernoulli_distribution dist;
std::cout << "Enter Number of flips: ";
std::cin >> n;
std::vector<bool> vec;
vec.reserve(n);
int heads = 0, tails = 0;
std::stringstream ss;
for ( int i = 0; i < n ; ++i) {
vec.push_back( dist(mt) );
if ( vec[i] ) {
++heads;
ss << "H";
} else {
++tails;
ss << "T";
}
}
std::cout << "Total heads : " << heads << " Total tails : " << tails
<< "\n" << ss.str() << std::endl;
return 0;
}