iT邦幫忙

2

程 式小白想要請求各路大神支援

  • 分享至 

  • twitterImage

生日悖論(python)
要多少人在場,「兩人同月同日出生」的機率才會大過80%?
並且用亂數生成方式證明
拜託大家了

淺水員 iT邦大師 6 級 ‧ 2021-10-08 01:33:10 檢舉
悖論的意思請參考維基百科
https://zh.wikipedia.org/wiki/%E6%82%96%E8%AE%BA
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
asd3733653
iT邦新手 2 級 ‧ 2021-10-07 11:42:46
最佳解答

A:大概34-35人
生日問題

https://zh.wikipedia.org/wiki/%E7%94%9F%E6%97%A5%E5%95%8F%E9%A1%8C

可看到 經證明 機率大概是 P = 1 * 1 - (1 / 365) * 1 - (2 / 365) ......以此類推
簡化如下:
https://ithelp.ithome.com.tw/upload/images/20211007/20137303W7Ig9oRGnK.png
亂數生成證明:
我們用 1 - 365 來代表一年當中不同的生日
在不考慮閏年的狀況下

import random
def test(n):
    count = 0
    # FOR迴圈測試100次
    for i in range(100):
        y = []
        for i in range(n):
            x = random.randint(1, 365)
            y.append(x)
        if len(y)!=len(set(y)):
            count+=1
    # 機率
    print(count/100)
test(34)

我自己測試幾次,機率大概都有到80%

4
海綿寶寶
iT邦大神 1 級 ‧ 2021-10-07 11:51:45

這個不用程式大神
Google 大神就有了

我要發表回答

立即登入回答