1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Birthday probability

  1. Jul 17, 2010 #1
    suppose that people enter an empty room one by one until a pair of people share a birthday. on average, how many people will enter before there is a match?

    my approach was to have 200 people entering; the birthdays of the first 100 people were stored into one array while the second 100 people's birthdays were stored in a different array. then i compared these arrays until they had the same value. my problems are -- 1) i'm not sure if my code is right for the approach i have, 2) i want to get an average so would the best way be move what i already have inside another for loop, 3) should the value be the same every time i execute it?

    Code (Text):

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    int figure(int s[], int p[]);

    int main(){
        const int MAX_SIZE = 100;
        int p[MAX_SIZE] = {0};
        int s[MAX_SIZE] = {0};
        srand(time(0));
        for (int i = 0; i < MAX_SIZE; i++){
            s[i] = rand()%364;
        }
        for (int k = 0; k < MAX_SIZE; k++){
            p[k] = rand()%364;
        }
        cout << figure(s,p);
    }

    int figure(int s[], int p[]){
        for (int j = 0; j < 100; j++){
            if (s[j] == p[j])
                return j;
                break;
        }
    }
     
     
  2. jcsd
  3. Jul 18, 2010 #2

    Mark44

    Staff: Mentor

    1) Your approach is a little off, but not too far off, but it only does one calculation.
    2) To get an average, keep a counter variable in main, and call your figure function inside a loop that runs some number of iterations.
    3) Not sure what you're asking here.
    Other comments:
    1) Make your MAX_SIZE constant global so you can use it in figure(), instead of hard-coding 100 in your loop.
    2) Why use two loops to initialize your two arrays? You can do both in one loop, like this:
    Code (Text):

    for (int i = 0; i < MAX_SIZE; i++)
    {
       s[i] = rand()%364;
       p[i] = rand()%364;
    }
     
    I favor loop and other braces on their own lines (which is something of a religious preference, but makes it easier IMO to make sure they match), and much less indentation.
    3) Your random numbers will be in the range 0...363. You want them in the range 1 ... 365. If you have rand()%365, the range will be 0...364. Someone showed you how to do this in another thread recently.
     
  4. Jul 19, 2010 #3
    i tried to run figure in a loop, but i get 0 as the answer. what else do i need to fix?

    Code (Text):

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    int figure(int s[], int p[]);
    const int MAX_SIZE = 1000;

    int main(){
        int p[MAX_SIZE] = {0};
        int s[MAX_SIZE] = {0};
        srand(time(0));
        for (int i = 0; i < MAX_SIZE; i++){
            s[i] = rand()%364;
        }
        for (int k = 0; k < MAX_SIZE; k++){
            p[k] = rand()%364;
        }
        for (int j = 0; j < MAX_SIZE; j++){
            figure(s,p);
        }
        cout << figure(s,p)/MAX_SIZE;
    }

    int figure(int s[], int p[]){
        for (int j = 0; j < MAX_SIZE; j++){
            if (s[j] == p[j])
                return j;
                break;
        }
    }
     
     
  5. Jul 19, 2010 #4

    Mark44

    Staff: Mentor

    You haven't addressed some of the points I mentioned in my previous mail, some of which will cause incorrect output. Please take another look at what I wrote before.

    1. You are calling figure() as if it were a void function. In this loop
    Code (Text):
    for (int j = 0; j < MAX_SIZE; j++){
       figure(s,p);
    }
    you call figure() MAX_SIZE times, but each time you are essentially discarding the return value. As I said before, you need to have a variable, say total, that you increment with the calculated value, like this.
    Code (Text):
    total += figure(s, p);
    Be sure you initialize total to 0.

    2. The expression in this statement
    Code (Text):
    cout << figure(s,p)/MAX_SIZE;
     
    will be zero if figure(s, p) is less than MAX_SIZE for the same reason that 4/3 == 1 and 5/6 == 0 in C and C++. There are two kinds of division--integer division and floating point division. If you want the result to be floating point division, make at least one of the expressions involved float or double, using a cast.
     
  6. Jul 19, 2010 #5
    hmm.. i am getting a different number every time.
     
  7. Jul 19, 2010 #6

    Mark44

    Staff: Mentor

    What do you mean, "every time?" Each iteration of the loop that calls figure() or each time you run the program? You're dealing with (pseudo)random numbers, so some variation should be expected.
     
  8. Jul 20, 2010 #7

    CEL

    User Avatar

    With 30 people, you have a probability of more than 50% that two or more of them share their birthday.
    If your program shows a significantly different figure, it must be wrong.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Birthday probability
  1. Probability of Failure (Replies: 3)

  2. Probability program (Replies: 6)

  3. Find probability (Replies: 3)

Loading...