【问题标题】:Birthday paradox python - incorrect probability output生日悖论python - 不正确的概率输出
【发布时间】:2017-10-12 12:37:08
【问题描述】:

我在用 Python 编写生日悖论时遇到问题。生日悖论基本上是说,如果一个班有 23 个人,那么他们两个生日相同的概率是 50%。

我尝试在 Python 中编写这个悖论,但它不断以接近 25% 的概率返回。我对 Python 很陌生,所以毫无疑问,这个问题有一个简单的解决方案。这是我的代码:

import random


def random_birthdays():
    bdays = []
    bdays = [random.randint(1, 365) for i in range(23)]
    bdays.sort()
    for x in bdays:
        while x < len(bdays)-1:
            if bdays[x] == bdays[x+1]:
                print(bdays[x])
                return True
            x+=1
        return False

count = 0
for i in range (1000):
if random_birthdays() == True:
    count = count + 1


print('In a sample of 1000 classes each with 23 pupils, there were', count, 'classes with individuals with the same birthday')

【问题讨论】:

  • 你做了什么调试?要求我们从您那里全面调试您的代码是不合适的。
  • 而你第二个问题的答案是单一责任原则。

标签: python birthday-paradox


【解决方案1】:

另外,你的函数应该是这样实现的:

import random

def random_birthdays(pupils):
    bdays = [random.randint(1, 365) for _ in range(pupils)]
    return pupils > len(set(bdays))

这消除了很多错误来源。

这可以称为@Zefick 指出的:

count  = sum(random_birthdays(23) for _ in range(1000))

【讨论】:

    【解决方案2】:

    这一行有错误:

    for x in bdays:
    

    应该是

    for x in range(len(bdays)):
    

    因为您需要遍历生日索引而不是生日本身。

    还有一项优化:

    count = 0
    for i in range (1000):
        if random_birthdays() == True:
           count = count + 1
    

    可以替换为

    count  = sum(random_birthdays() for _ in range(1000))
    

    【讨论】:

      【解决方案3】:
      import math
      def find(p):
          return math.ceil(math.sqrt(2*365*math.log(1/(1-p))));
      
      print(find(0.25))
      

      【讨论】:

      • 感谢您的回答。但是,请考虑添加一些解释,以便提出问题的人能够更好地理解您。
      【解决方案4】:

      我是这样写的。

      # check probability for birthday reoccurance for a class of 23 students or the birthday paradox
      import random as r
      
      def check_date(students):
          date=[]
          count=0
          
          for i in range(students): # Generate a random age for n students
              date+=[r.randint(1,365)] # entire sample list for age is created
              
          for letter in date: # check if the date repeats anywhere else
              if date.count(letter)>=2: # Use count it's simple & easy.
                  count+=1
                  
          return count # count of a pair of students having same b.day
      
      def simulations(s,students):
          result=[] # empty list to update data.
          simulation_match=0
          
          for i in range(s):
              result+=[check_date(students)] # get a sample list for all the students in 'n' no. of simulations
              
              if check_date(students)>1: # if atleat 2 students have same b.day in each simulation
                  simulation_match+=1
                  
          return simulation_match,s,int(simulation_match/s*100),'%'
      
      simulations(1000,23) # 1000 simulations with 23 students sample size
      

      OUT:(494, 1000, 49, '%') **百分比部分根据生成的随机 int 而有所不同**

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-23
        • 1970-01-01
        相关资源
        最近更新 更多