陈述/计算的生日悖论 [英] Birthday paradox for statement/calcutions

查看:63
本文介绍了陈述/计算的生日悖论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,我实际上是想创建一个数组,该数组在许多次试验中随机分配生日(5000).然后,假设每次至少有2个生日(可容纳2至50个人)时将其计数,然后将结果除以5,000,以得出大概的概率.我相信自己的循环很混乱,并希望得到一些反馈.不是代码,我想确切地了解出了什么问题以及如何弄糟.

Apparently I was actually suppose to create an array that randomly assigns birthdays over many trials (5000). It's then suppose to count up each time there is at least 2 birthdays for 2 - 50 people and divide the outcome by 5,000 to get the approximate probability. I believe I have my loops messed up and would like some feedback. Not code, I would like to understand exactly what is going wrong and how I messed it up.

int main()
{
    const int trials(5000);
    double total;
    int count(0), birthdays[49];

    srand(time(NULL));

    for (int i = 2; i <= 50; i++)
    {
        for (int k = 0; k < trials; k++)
        {
            fillUp(birthdays, 49);
            for (int j = i + 1; j <= 50; j++)
            {
                if (birthdays[i] == birthdays[j])
                {
                    count += 1;
                }
            }
        }
        total = count / 5000.0;
        cout << "For " << i << " the probability is " << total << endl;
    }
    return 0;
}

void fillUp(int birthdays [], int size)
{
    for (int i = 0; i < size; i++)
    {
        birthdays[i] = rand() % 365 + 1;
    }
}

输出:

For 2 the probability is 0.1286
For 3 the probability is 0.2604
...
...
For 49 the probability is 3.9424
For 50 the probability is 3.9424

任何帮助将不胜感激.

推荐答案

实质上,您已经创建了一个新问题,因此我将创建一个新答案.现在,您在遍历生日时不断更改生日;这就是为什么事情不起作用的原因.您需要两个嵌套循环来测试相等的生日(或者,如果您很聪明,可以对它们进行排序,然后仅检查相邻的生日.n= 50可能更快.)

In essence you have created a new question, so I will create a new answer. Right now you keep changing the birthdays while you are looping over them; this is why things don't work. You need two nested loops to test for equal birthdays (or if you are smart you sort them, then only check adjacent ones. It is probably faster with n = 50.)

您还需要在第一个生日开始测试(您的数组以0为底-但您从i = 2开始).对于每个审判,您都可以看到有一场比赛之前,需要比较多少人.正确的代码应如下所示(请注意,我对房间中的每个人进行了5000次试用;如果您有3,4,5个...基于相同的人,则通过检查匹配项来提高效率样本,但样本中会有一些相关性.

You also need to start testing at the first birthday (your array is base 0 - but you start with i = 2). And for each trial, you can see how many people you need to compare before you have a match. The correct code will look something like this (note that I run 5000 trials for each number of people in the room; you could be more efficient by checking for a match when you have 3,4,5... people based on the same sample, but then there would be some correlation in the sampling).

已编辑-测试了此代码,似乎可以编译并运行正常.结果看起来接近预期值.

EDITED - tested this code, seems to compile and run OK. Results look close to expected values.

#include <iostream>

void fillUp(int birthdays [], int size)
{
    for (int i = 0; i < size; i++)
    {
        birthdays[i] = rand() % 365 + 1;
    }
}

int main(void) {
int birthdays[50];
int trials = 5000;
int flag;
double total;
int collisions[50];
   // number of people "in the room"
   for (int i = 2; i < 50; i++)
      {
      collisions[i] = 0;
      // do 5000 trials:
      for (int t = 0; t < trials; t++)
       {
        fillUp(birthdays, i);
        flag = 0;
        // compare all pairs (j,k):
        for (int j = 0; j < i - 1 && flag == 0; j++)
        {
          for (int k = j + 1; k < i && flag == 0; k++ )
          {
            if (birthdays[k] == birthdays[j])
            {
              collisions[i]++;
              flag = 1;
            }
          }
        }
      }
    total = collisions[i] / 5000.0;
    std::cout << "For " << i << " people in the room the probability is " << total << std::endl;
    }
return 0;
}

注意-我没有机会进行编译/测试;本质上"应该是正确的.让我知道是否有问题.

Note - I did not have a chance to compile / test this; it should be right "in essence". Let me know if it gives a problem.

这篇关于陈述/计算的生日悖论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆