我在编程时遇到了Python中生日悖论的问题。生日悖论基本上说,如果一个班有23个人,那么他们中的两个将有相同的生日的概率是50%。生日悖论python - 不正确的概率输出
我试图在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')
2017-10-12
Alcor
+3
你做了什么样的调试?要求我们全面调试您的代码并不合适。 –
+1
第二个问题的答案是单一责任原则。 –