-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthday.c++
63 lines (52 loc) · 1.16 KB
/
birthday.c++
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
#include <iostream>
#include <random>
#include <ctime>
#include <fstream>
using namespace std;
void simulate(int trial_students, int total_values)
{
int test_iterations = 100000;
int favourable_count=0;
for(int iter = 1; iter<=test_iterations; iter++)
{
int test[total_values];
memset(test, 0, sizeof(test));
for(int student = 1; student<=trial_students; student++)
{
int index = rand()%total_values;
if(test[index]==1)
{
favourable_count++;
break;
}
else
{
test[index]++;
}
}
}
double prob = (double)favourable_count/((double)test_iterations);
if(trial_students%10 == 0)
{
cout<<"\tTrial Students = "<<trial_students<<endl;
cout<<"\tProbability of Collision = "<<prob<<endl;
cout<<endl;
}
// For graph plotting
// ofstream fout;
// string name = "data.txt";
// fout.open(name, ios_base::app);
// fout<<trial_students<<"\t"<<prob<<endl;
// fout.close();
}
int main(int argc, char const *argv[])
{
srand(time(NULL));
int total_values = 365;
cout<<"Total Values = 365"<<endl<<endl;
for(int trial_students = 2;trial_students<=100;trial_students++)
{
simulate(trial_students, total_values);
}
return 0;
}