Thread: Birthday Paradox Code

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Birthday Paradox Code

    Hey, I have done some code for the birthday paradox, but I keep getting weird percentages like 3.3 or 2.5 (which is supposed to be about 70% with 30 guests)..
    Here's what I have done...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 366
    
    int party (int n);
    main()
    {
        int trial, bday, n, guests;
        float percent, total, i;
        printf("How many parties are there? ");
        scanf("%d", &trial);
        printf("How many guests attend? ");
        scanf("%d", &guests);
        srand(time(NULL));
        for (i=1; i<=trial; i++)
        {
            bday = party(n);
        }
        if (bday == 1)
        {
            total += 1;
        }
        percent = (float)total/guests*100.0;
        printf("The Probablity of people with the same birthday is %0.2f\n", percent);
        }
        int party (int n)
        {
        int days;
        int frequency [SIZE] = {0};
        days = 1 + rand() % 365;
        ++frequency[days];
        if (frequency[days] <= 2)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    Any help will be appreciated, thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your party function doesn't use n at all -- it will always return 1.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int party (int n)
    You don't use n

    > int frequency [SIZE] =
    This has no memory of previous calls.

    > if (frequency[days] <= 2)
    How is frequency[days] anything other than 1 ?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM