LoginSignup
0
0

More than 3 years have passed since last update.

「誕生日のパラドックス」をPythonでシミュレーションしてみた

Last updated at Posted at 2020-02-03

何人集まれば、その中に誕生日が同一の2人(以上)がいる確率が、50%を超えるか?
というアレ。

  • 集団の中に同じ誕生日の組み合わせの人がいる確率が50%を超えるのは23人から。
    • 70人集まると99.9%を超える

誕生日のパラドックス - Wikipedia

70人しか集まらなくても確率は99.9%を超え、50%を超えるのに必要なのはわずか23人である。

※閏年や双子は考慮しません

birthday_paradox.py
import math
from random import random


n_trials = 50000  # 1回あたりの試行回数
max_people = 100  # 検証する最大人数


print('人数,同一誕生日の組があった回数,試行回数,確率')
for n_people in range(2, max_people + 1):
    count = 0  # 同じ誕生日の人がいた回数
    for _ in range(n_trials):
        # 誕生日に見立ててた1~365 の乱数整数をリストに格納する。
        births = []
        for __ in range(n_people):
            birthday = math.ceil(random() * 365)
            births.append(birthday)
        # 重複を除いた要素数が元の要素数と一致しなければ誕生日の重複があるということ
        if len(births) != len(set(births)):
            count += 1
    print(f'{n_people},{count},{n_trials},{count / n_trials}')

実行結果

人数,同一誕生日の組があった回数,試行回数,確率
2,105,50000,0.0021
3,390,50000,0.0078
4,773,50000,0.01546
5,1370,50000,0.0274
6,1992,50000,0.03984
7,2853,50000,0.05706
8,3686,50000,0.07372
...
17,15850,50000,0.317
18,17422,50000,0.34844
19,18898,50000,0.37796
20,20602,50000,0.41204
21,22207,50000,0.44414
22,23795,50000,0.4759
23,25531,50000,0.51062  # <- 23人を超えたところで50%を超えた
24,27170,50000,0.5434
25,28517,50000,0.57034
26,29820,50000,0.5964
27,31400,50000,0.628
...
68,49925,50000,0.9985
69,49940,50000,0.9988
70,49952,50000,0.99904  # <- 70人を超えたところで99.9%を超えた
71,49971,50000,0.99942
72,49970,50000,0.9994
73,49971,50000,0.99942
74,49985,50000,0.9997
75,49987,50000,0.99974
76,49989,50000,0.99978
77,49994,50000,0.99988
78,49994,50000,0.99988
79,49994,50000,0.99988
...
99,50000,50000,1.0
100,50000,50000,1.0  #  <- もはや100%!!
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0