i was assigned a short project in c++ today
and i thought that i would share the code that calculates the birthday paradox
#include <iostream>
#include <cstdlib>
using namespace std;
double birthdayCollecter(int B[],int n,double t)
{
int C;
C=0;
for (int h=1; h<=t; h++)
{
for (int i=0; i<n; i++)
{
bool breaker = false;
B[i] = (rand() %365 + 1);
cout<<"\nB["<<i<<"] = "<<B[i];
for (int j =0; j<i; j++)
{
if (B[j] == B[i])
{
C++;
cout<<" "<<C;
breaker = true;
break;
}
}
if (breaker == true){break;}
}
}
return C;
}
int collectPeople()
{
cout<<"how many people would you like to check at once : ";
int people;
cin>>people;
while (people>100)
{
cout<<"\nSorry pick a number lower then 100, the percent it too high otherwise\n";
cout<<"how many people would you like to check at once : ";
cin>>people;
if (people<100){return people;}
}
return people;
}
int main()
{
//this is the intoduction message, it asks the user to input the wanted values
cout<<"\nwelcome to the birthday paradox calculator \n";
// the number of people to be tested is input and checked
int numPeople = collectPeople();
cout<<"\nok you want to check "<< numPeople <<" People \n";
// the number of wanted simulations is imput
cout<<"how many times do you want to run the simulation : ";
double trials;
cin>> trials;
double Count;
int birthdayArray[numPeople];
// The count of how manny people share birthdays is defined by the Birtday Collecter function
Count = birthdayCollecter(birthdayArray,numPeople,trials);
cout<<"\nthe Probablility of two people in a room of "<<numPeople<<" sharing a birthday is : "<< (Count/trials)*100<<"%" ;
cout<<"\n\n";
>not using just a return statement
>instead of a stupid break trick
Name:
Anonymous2011-01-21 6:42
You didn't use code tags so unfortunately I'm unable to review the code ... But even so I'm sure I could write this in a much more effecient manner as you are obviously a noob
#include <iostream>
#include <cstdlib>
using namespace std;
double birthdayCollecter(int B[],int n,double t)
{
int C;
C=0;
for (int h=1; h<=t; h++)
{
for (int i=0; i<n; i++)
{
bool breaker = false;
B[i] = (rand() %365 + 1);
cout<<"\nB["<<i<<"] = "<<B[i];
for (int j =0; j<i; j++)
{
if (B[j] == B[i])
{
C++;
cout<<" "<<C;
breaker = true;
break;
}
}
if (breaker == true){break;}
}
}
return C;
}
int collectPeople()
{
cout<<"how many people would you like to check at once : ";
int people;
cin>>people;
while (people>100)
{
cout<<"\nSorry pick a number lower then 100, the percent it too high otherwise\n";
cout<<"how many people would you like to check at once : ";
cin>>people;
if (people<100){return people;}
}
return people;
}
int main()
{
//this is the intoduction message, it asks the user to input the wanted values
cout<<"\nwelcome to the birthday paradox calculator \n";
// the number of people to be tested is input and checked
int numPeople = collectPeople();
cout<<"\nok you want to check "<< numPeople <<" People \n";
// the number of wanted simulations is imput
cout<<"how many times do you want to run the simulation : ";
double trials;
cin>> trials;
double Count;
int birthdayArray[numPeople];
// The count of how manny people share birthdays is defined by the Birtday Collecter function
Count = birthdayCollecter(birthdayArray,numPeople,trials);
cout<<"\nthe Probablility of two people in a room of "<<numPeople<<" sharing a birthday is : "<< (Count/trials)*100<<"%" ;
cout<<"\n\n";
cout<<"how many times do you want to run the simulation : ";
double trials;
double? really?
int birthdayArray[numPeople];
I think the only reason why he allocates data on stack is because he's unaware of vector
double Count;
sometimes he start variables with UPPERCASED letter, sometimes not. Sometimes he uses long varnames(Count), sometimes short(C).
This clearly mean that kid is still questioning his sexuality.
>>7
what if(people == 100) ?
Walso why check that?
Name:
Anonymous2011-01-21 12:53
Suggestion: have the array of birthdays 365 units long and count the number of people using a single int. That way you can generate the birthday value and increment the array index of that birthday. After you have done that to for all the people, then search through the array once for any index with a value greater than or equal to 2.
The run time of this function is a consistent (people - 1 + 365)*trials so it obviously benefits over your method for higher numbers of people (but not specifically for smaller ones) by having to iterate fewer times.
25 people and 1 trial, 300 compared to 390.
50 people and 1 trial: 1225 compared to 415.
100 people and 1 trial: 4950 compared to 465.
Try to imagine how 1000 people would compare.
>>8
it's redundant. OP has a while already. >>12
because you touch yourself right now
Name:
OP2011-01-23 16:06
oops i really feel like i should have qualified this post now. this was the first time i have ever posted on this particular forum, and this is the second code i ever wrote in C++, as far as knowledge of the language goes mine is extremely minimal, the only reason i posted this thread at all was because of the strange out come when the test is actually run.
the birthday paradox is the paradox that if you have a relitivly small number of people in the room the chances of two people sharing a birthday is very large, one of the most specific examples is that with only 23 people in the room the probability of two people sharing a birthday is 50% !! this program generates sudo random birthdays and shows the probability at the end. im going to look up how to post in actual programming format and then post the current code which generates a seed for the random based on the time and also saves all the end data in an array for easy comparison when multiple tests have been preformed.
## this is more of a fun thing then anything else there are birthday paradox calculators on the net if you don't feel like taking 3 seconds to compile the code
I am the first time on this site and am really enthusiastic about and so many good articles. I think it’s just very good. http://www.ahhermesbags.com/ birkin bag
Fashion handbags to http://www.ahhhermesbags.com ,Get 5% discount, use coupon code "ahhh" before 31th July ,hermes birkin bag give you a super surprise!
Would you like to have a hermes birkin or a hermes kelly? yes, It is a good place you have coming, there have a good store which sell top quality hermes bags, anything you want will provide,all of them made of top grade real leather, but cheap price, if you are interest in it ,I think it is the perfect store, you can have look it. http://www.wallyhermesbags.com