Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Birthday Paradox

Name: Anonymous 2011-01-21 2:36

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";
   

    return 0;
}

Name: OP 2011-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

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List