Birthday Paradox

Hi, I am new to C++ and I am trying to teach myself. I am currently working on the Birthday Paradox. My code is below, I think the way I am initializing the arrays correctly. If anyone can help me I would greatly appreciate it.
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
 #include <cstdlib>
 #include <iostream>
 #include <ctime>

  using namespace std;



int main()
{
    srand(time(NULL));

    const int trials = 10000;
    int birthdays[50];
    int numMatches;
    

    for(int i = 2; i <= 50; i++)
    {
        numMatches = 0;
        for(int j = 0; j <= trials; j++)
        {
            for(int k = 2; k <= 50; k++)
            {
                birthdays[i] = (rand() % 365) + 1;
                birthdays[k] = (rand() % 365) + 1;
                if(birthdays[i] == birthdays[k])
                    numMatches++;
                cout << birthdays[i] << "snarf" << endl;
                cout << birthdays[k] << endl;
                cout << numMatches << endl;
            }
        }

        cout << "Probability of " << i << " people in a room sharing a birthday is \t"
          << ( float(numMatches) / float(trials) ) << endl;
    }  
}
Last edited on
array index goes from 0 to size-1
in your case you may access birthdays[0] to birthdays[49] trying to use birthdays[50] is a logic error that causes undefined behaviour.

line 27 would be executed (trials+1)*49 times. So you may have a probability greater than 1.

I don't understand your logic, please write the pseudocode.


> If anyone can help me I would greatly appreciate it.
be clear about what you need help with (erroneous results, crash, compilation errors, logic)
Last edited on
Hi, sorry I am new to programming in c++. Specifically, the probabilities are incorrect. My logic is as follows:
1) I create a for loop to loop through each 2 through 50 people and set the number of matches to 0.
2) Then I create another for loop to iterate the process of assigning random birthdays to the i and k people.
3) Then I compare to see if there is a match between the i peoples birthdays and the k peoples birthdays
Does that logic make sense?
> I create a for loop to loop through each 2 through 50 people
iirc you want to answer the probability that in a room of say 13 people there are at least two that share a birthday.
If you group is only 13, ¿why do you go through 50 people?

> assigning random birthdays to the i and k people.
¿how many times would you give a birthday to the `i' person?
¿what happens if i==k? Notice how the check on line 27 would be always true.

This is what you are doing:
- i goes from 2 to 50. There is no special meaning attached to it.
- A lot of times do:
--- Generate two random number and see if they are the same
- Divide the number of matches by 10000


Your description is too specific, you need to abstract more. By instance
- Create a group of n people
- Assign each one of them a birthday.
- Check for duplicate
Topic archived. No new replies allowed.