0

我在用 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')
4

4 回答 4

2

此外,您的功能应该这样实现:

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))
于 2017-10-12T12:47:02.547 回答
1

此行中的错误:

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))
于 2017-10-12T12:43:56.410 回答
0
import math
def find(p):
    return math.ceil(math.sqrt(2*365*math.log(1/(1-p))));

print(find(0.25))
于 2020-06-24T18:23:02.743 回答
0

我是这样写的。

# 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 而有所不同**

于 2020-06-25T19:57:34.410 回答