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

This is a discussion on If your familiar with probabilities and the birhday paradox (help me ): ) within the C Programming forums, part of the General Programming Boards category; Hello,i tried to solve the birthday problem with probabilities and i have a problem,there is an error and i cant ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Lightbulb 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;
    }
    Last edited by dionys; 04-12-2004 at 01:01 PM. Reason: expand

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    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
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    i already told you ... if i knew exactly i wouldnt sent a thread
    you are very demanding sorry...

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Quote Originally Posted by dionys
    With the first loop my program is a total disaster.
    Expand

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    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
    made it an int which just made more sense to me...
    Code:
    #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;
    }
    DavT
    -----------------------------------------------

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    thanks a lot
    thanks
    thanks

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21