Birthday Paradox Code

This is a discussion on Birthday Paradox Code within the C Programming forums, part of the General Programming Boards category; Hey, I have done some code for the birthday paradox, but I keep getting weird percentages like 3.3 or 2.5 ...

  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,185
    Your party function doesn't use n at all -- it will always return 1.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 04:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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