使用布尔函数的C ++生日悖论

时间:2015-08-29 18:13:41

标签: c++ boolean birthday-paradox

我有一项任务,我需要计算两个人在许多试验(5000)中为给定房间大小(在我的情况下为50)共享同一个生日的概率。我必须将生日随机分配给房间里的人数。不同之处在于我需要使用布尔函数来检查生日是否相同。我无法理解为什么我的输出关闭,但我相信它与我的两个循环有关。

> 

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

    bool SameBirthday(int birthdays[], int numpeople);
    const int MAX_PEOPLE = 50;
    const double NUM_TRIALS = 5000.0;
    const int DAYS_IN_YEAR = 365;

    int main(void)
    {
        int numMatches = 0;
        int people = 2;
        int trial = 0;
        int numpeople = 0;
        int i = 0;
        int birthdays[MAX_PEOPLE];
        bool Match;
        double Probability = 0;
        srand(time(0));
    for (people = 2; people <= MAX_PEOPLE; people++)
        {
            numMatches = 0;

            for (trial = 0; trial < NUM_TRIALS; trial++)
            {
                for (i = 0; i < people; i++)
                {
                    birthdays[i] = (rand() % 365 + 1);
                    numpeople = i;

                }
                if ((SameBirthday(birthdays, numpeople) == true))
                    {
                        numMatches++;
                    }
            }
            Probability = (numMatches / NUM_TRIALS);
            cout << "For " << people << ", the probability of two birthdays is about " << Probability << endl;
        }
    }
    bool SameBirthday(int birthdays[], int numpeople)
    {

        bool match = false;
        int numberofmatches = 0;
        //Use this function to attempt to search the giving array birthdays and   count up number of times
        //at least two people have matching birthdays for any given 1 trial
        for (int SpaceOne = 0; SpaceOne < numpeople; SpaceOne++)
        {
            for (int SpaceTwo = SpaceOne + 1; SpaceTwo < numpeople; SpaceTwo++)
            {
                if (birthdays[SpaceTwo] == birthdays[SpaceOne])
                {
                    return true;
                }
            }
        }
return false;
    }

    I know that the code has errors in certain spots that was because I started trying different things, but any help would be appreciated. 
EDIT- My only issue now is that for my output I have a zero for the probability of 2 people in the room have a birthday, which is not right. It seems like my outputs are like a person off, the probability of 2 people is shown as the probability for three people and so on. 
EDIT(8-31-2015): I also forgot to mention that my Professor stated that my SameBirthday function needed the parameters: birthday[], and numpeople so I cannot use MAX_PEOPLE as a parameter. My professor also suggested using a triple nested for loop within the main body of the function. I believe what is making my output off by one for each person relates to the triple nested for loop, but I am unsure what would cause the issue.

4 个答案:

答案 0 :(得分:2)

就这样做:

bool SameBirthday(int birthdays[], int numPeople)
{
    for(int x=0; x<numPeople; x++){         
        for(int y=0; y<numPeople; y++){
            if(birthdays[x] == birthdays[y])
                return true;
        }
    }
    return false;
}

你的嵌套循环中的逻辑是错误的。

for (SpaceOne = 0; SpaceOne < numpeople - 1; SpaceOne++)
    for (SpaceTwo = SpaceOne + 1; SpaceTwo < numpeople; SpaceTwo++)

您的内部循环正在跳过nn等于SpaceOne的检查。

顺便说一句,这不是C编程。您可以在for循环中声明变量。

答案 1 :(得分:0)

我发现实际功能存在两个问题。首先,SameBirthday需要在没有生日匹配时返回值(false)。完成所有循环后,您可以在函数结束时执行此操作。

其次,当您找到匹配项时,需要增加numMatches。

答案 2 :(得分:0)

澄清编码其他部分的问题。我想这就是你学校想要的。

int main(){
    //All your variables

    for(int x=0; x<NUM_TRIALS; x++){
        for(int y=0; y< MAX_PEOPLE; y++){
            birthdays[y] = (rand() % 365 + 1);
        }

        if(SameBirthday(birthdays, MAX_PEOPLE) == true)
            numMatches ++;              
    }
    Probability = ((double)numMatches / NUM_TRIALS);
    cout << "For " << people << ", the probability of two birthdays is about " 
    << Probability << endl;     
}

NUM_TRIALS 以生成5000个数据集。因此,您为50名学生生成5000次生日。对于50级以上的每个试验,您检查是否有2个人具有相同的生日。如果有,numMatches + 1

经过5000次试验,你得到概率。

答案 3 :(得分:0)

你的另一个问题是,numpeople总是减去1的人数。你根本不需要那个变量。你的“人”变量是正确的人数。