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

Pages: 1-

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: Anonymous 2011-01-21 4:19

That's not Lisp.

Name: Anonymous 2011-01-21 4:44

>>2
That's autism.

Name: Anonymous 2011-01-21 6:15

>not using just a return statement
>instead of a stupid break trick

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

Name: Anonymous 2011-01-21 6:47

>>4
not using sage
nor quoting properly


>>5
I'll help >>1 and codeify it


#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;
}


Now I shell review.

Name: Anonymous 2011-01-21 6:56

shell

oh my.

Back to code review

birthdayCollecter(int B[]

javafag

for (int h=1; h<=t; h++)

counts from 1
uses h<=t instead of h <= t

breaker = true;

No return statement?


if (people<100){return people;}

redundant check is redundant.

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.

// The count...
    Count

no indentation of comments.

Probability of faggot: >100%

Here goes my sage.

Name: Anonymous 2011-01-21 9:32

>>7
what
if(people == 100) ?
Walso why check that?

Name: Anonymous 2011-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.

Name: Anonymous 2011-01-21 13:06

ONE WORD: NP-COMPLETE. CHALLENGE OVER

Name: Anonymous 2011-01-21 13:38

1-n!C(365,n)/365^n

EXPERT OMG OPTIMIZED O(1) TIME SOLUTION

Name: Anonymous 2011-01-21 13:48

Why is it called the birthday paradox? I have trouble identifying the paradoxical part.

Name: Anonymous 2011-01-21 13:49

>>12
Autist detected.

Name: Anonymous 2011-01-21 13:50

>>12
A birthday paradox is when it's your birthday and you don't sage.

Name: Anonymous 2011-01-21 15:09

>>14
like this?

Name: Anonymous 2011-01-21 23:51

>>8
it's redundant. OP has a while already.
>>12
because you touch yourself right now

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

Name: Anonymous 2011-01-23 16:11

>>17
Before writing your next ``project'' I suggest you learn proper spelling and punctuation. This is a very important skill that you need to master.

Name: VIPPER 2011-01-23 17:32

>>18
I will pray for your death.

Name: VIPPAR 2011-01-23 17:46

JAWS

Name: Anonymous 2011-01-23 17:50

>>1
might i suggest that at least for the time being, you post less and lurk more?

Name: Anonymous 2011-01-23 17:50

>>17
also, you didn't sage!

good day sir

Name: Anonymous 2011-01-24 6:49

>>22: derp

Name: Anonymous 2011-01-24 6:57

!>>22: derp
:>>22: derp

Name: Anonymous 2011-01-24 7:01

[b]this is now a saged test thread[/m]

Name: !!Cgk9X8wfP1B3TgY 2011-01-24 7:03

t

Name: Anonymous 2011-11-27 2:46

Wonderful.. I will bookmark your blog and take the feeds also…I am satisfied to find so much useful info here in the post. Thank you for sharing. Vogue beautiful and popular dancing party full dress, you are worth owning.
http://www.hermeshandbagoutlet.com
http://www.handbagsdreams.com
http://www.backpackunion.com
http://www.charmhandbags.com
http://www.pursehandbag.org

Name: hermes birkin 2012-04-14 9:39

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

Name: Anonymous 2012-04-14 20:34

my birthday is December 10, 1993

Name: Anonymous 2012-04-16 4:36

>>17

oops i really feel like i should have qualified this post now.
QUALIFY MY ANUS

Name: hermes birkin 2012-06-29 3:23

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!

Name: hermes birkin 2012-09-03 10:56

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

Name: Anonymous 2012-09-03 11:11

>>1-32
Your birthday paradox is that on every birthday you have a party, yet no one comes to it.

Name: Anonymous 2012-09-03 11:28

>>33
yeah i know, too bad he doesn't go on le reddit XD

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