-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate_coin_toss.cpp
More file actions
38 lines (31 loc) · 942 Bytes
/
simulate_coin_toss.cpp
File metadata and controls
38 lines (31 loc) · 942 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
/*
int rand(void):
returns a pseudo-random number in the range of 0 to RAND_MAX.
RAND_MAX: is a constant whose default value may vary
between implementations but it is granted to be at least 32767.
void srand( unsigned seed ):
Seeds the pseudo-random number generator used by rand()
with the value seed.
Standard practice is to use the result of a call to srand(time(0)) as the seed.
However, time() returns a time_t value which vary everytime and hence the
pseudo-random number vary for every program call.
*/
#include <iostream>
#include <stdlib.h>
#include <ctime>
int main() {
int count=0;
// Create a number that's 0 or 1
for(int i=0;i<5;i++)
{
srand (time(NULL));
// as we only want binary values
int coin = rand() % 2;
// If number is 0: Heads
// If it is not 0: Tails
if (coin == 0)
count++;
}
// output the number if occurences if Heads
std::cout << count <<"\n";
}