#include
#include
#include
#include
using namespace std;
//vars
int GroupOfPeople[50];
//bool v = true;
//funcs
void Fill (int M);
bool CheckDuplicates (int M);
double T_Math(int M);
int MaxSamples = 50000;
int main(){
//srand ( time(NULL) );
srand(1);//for debug; static seed
double ModelSum = 0;
double Modelavg = 0;
cout << setw(15) <<"Num in group"
< < < for (int M=1; M<50; M++) {//this loop performs checks for groups of size M
ModelSum=0;
for (int ii=0; ii Fill(M);//ensures a new sample each call
if (CheckDuplicates(M)){ ModelSum++;}//endif
}//end for
Modelavg=ModelSum/MaxSamples;
//printf("The Theoretical probability of duplicate birthdays in a group of %d is %f\n",M,T_Math(M));
//printf("The Modeled probability of duplicate birthdays in a group of %d is %f\n",M,Modelavg);
double TP = T_Math(M);
cout< cout << setw(15) <<(M +1)
< < cout< < }//end for
}//end main
void Fill(int M){
for (int i=1; i GroupOfPeople[i] = ((rand()%365)+1);
}//end for
}//end fill
bool CheckDuplicates (int M){
//nested loop for self comparison, we don't need more memory(M$, take note!)
for (int i=0; i< M; i++) {
for (int ii=(i+1); ii if (GroupOfPeople[i]==GroupOfPeople[ii]) {
return true;
}//end if
}//end for
}//end for
return false;
}//end CheckDuplicates
double T_Math(int M){
double a = ( 364 / (static_cast ( 365 ) ) );
double e = ( ( M * ( M - 1 ) ) / (static_cast ( 2 ) ) );
return 1 - pow(a, e);
}//end T_Math
Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/GmsIzCRGKnU/12955