Thread: Birthday Paradox Code

  1. #1
    Registered User
    Join Date
    Mar 2010

    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...

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 366
    int party (int n);
        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);
        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;
        if (frequency[days] <= 2)
            return 1;
            return 0;
    Any help will be appreciated, thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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
    The edge of the known universe
    > 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, 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
Website Security Test