# If your familiar with probabilities and the birhday paradox (help me ): )

• 04-12-2004
dionys
If your familiar with probabilities and the birhday paradox (help me ): )
Hello,i tried to solve the birthday problem with probabilities
and i have a problem,there is an error and i cant find it....
If you are patient plz look my source code and give me your ideas.

#1.This program returns the maximum number of people in a group
when at least 2 of them have a 50% probability to selebrate their birthday the same day.

#3.probality=success trials/number of trials

#4.the array gets random numbers in the interval [1,365] from rand()
I start with a group of 2 people ,the number of the group isnt bigger from 100 people.
#4a. number of people [2,100]

#5.I already solve this problem in a mathimatical form and i know for sure that the probability that at least 2 of them have a 50% to selebrate their birthday the same day when the group is composed by 23
people.

#6.i already isolated the problem but i dont know why this bring me an error )~~:

#7.The error is in the first for loop.If you munually put the number of people i.e people=23 you find the correct probab.(approximately 0.5).With the first loop my program is a total disaster.

Code:

```#include<stdio.h> #include<stdlib.h> int main() { int people,array[100],number,counter0,counter1,counter2; float probability,success_trials,limit=0.50,number_of_trials;           printf("Enter the number of trials\n"); scanf("%f",&number_of_trials);   for(probability=0.0,people=2;probability<limit;people++)       {         for (counter0=0;counter0<number_of_trials;counter0++)             {         for (counter1=0;counter1<people;counter1++)           {             number=array[counter1]=rand()%365+1;                   /*random numbers in the interval[1,365]*/                                                             for (counter2=0;counter2<counter1;counter2++)                 {                   if (number==array[counter2]) /*we compare the first array element with the others  and if we find a duplicated number then there  are at least two people with the same birthday*/                 {                           success_trials++;                                                    counter1=counter2=people;                                        }                      }                 }                                                             }           probability=success_trials/number_of_trials;   }  }```
#7.The error is in the first for loop.If you munually put the number of people i.e people=23 you find the correct probab.(approximately 0.5).With the first loop my program is a total disaster.put i need it to show the probability for 2,3,4...23 people :)

Code:

```#include<stdio.h> #include<stdlib.h> int main() { int people=23,array[100],number,counter0,counter1,counter2; float probability,success_trials,limit=0.50,number_of_trials;           printf("Enter the number of trials\n"); scanf("%f",&number_of_trials);             for (counter0=0;counter0<number_of_trials;counter0++)             {         for (counter1=0;counter1<people;counter1++)           {             number=array[counter1]=rand()%365+1;                   /*random numbers in the interval[1,365]*/                                                             for (counter2=0;counter2<counter1;counter2++)                 {                   if (number==array[counter2]) /*we compare the first array element with the others  and if we find a duplicated number then there  are at least two people with the same birthday*/                 {                           success_trials++;                                                    counter1=counter2=people;                                        }                      }                 }                                                             }           probability=success_trials/number_of_trials; printff("%f\n",probability); return 0; }```
• 04-12-2004
WaltP
Would you mind posting code that isn't 2 miles wide? Use spaces, not tabs. And give us better details on what is wrong, where it's wrong, and why it's wrong.

Use the PREVIEW button when posting
• 04-12-2004
dionys
i already told you ...:) if i knew exactly i wouldnt sent a thread
you are very demanding sorry...
• 04-12-2004
major_blagger
Quote:

Originally Posted by dionys
With the first loop my program is a total disaster.

Expand
• 04-12-2004
Salem
Perhaps if you didn't try and write it all in one go (a whole bunch of loops inside main), then perhaps the problem / solution would be clearer.

In other words, use some functions.

Oh yeah, WaltP is correct, its up to you to make sure your post is presentable. If it looks a mess, people will ignore it.
• 04-13-2004
DavT
Your problem was that you weren't resetting (or indeed setting)
the variable 'success_trials'. Here is the code re-formatted and
corrected. I also re-named 'number_of_trials' as 'n_trials' and
```#include<stdio.h> #include<stdlib.h> int main(void) {   int people, array[100], counter0, counter1, counter2, n_trials;   float probability, success_trials = 0, limit = 0.50;             printf("Enter the number of trials\n");   scanf("%d", &n_trials);   for(probability = 0.0, people = 2; probability<limit; people++) {     success_trials = 0;     for (counter0 = 0; counter0 < n_trials; counter0++) {       for (counter1 = 0; counter1 < people; counter1++) {         array[counter1] = rand()%365+1;         for (counter2 = 0; counter2 < counter1; counter2++) {           if (array[counter2] == array[counter1]) {             success_trials++;                                      counter1 = counter2 = people;                          }          }       }                                            }     probability = success_trials / n_trials;   }           printf("probability = %f when people = %d\n", probability, people-1);   return 0; }```