Birthday Paradox

Birthday Paradox: 23명이 모이면 그 안에 생일이 같은 사람이 존재할 확률은 50% 이상.

계산해보자 (Python)

   1 person = 23
   2 odd = 1.0
   3 
   4 for x in range(2,person+1):
   5     odd = odd * (366 - x)/365
   6     print x, "person:", (1-odd)*100, "%"

랜덤 생일 23개로 이루어진 그룹 10000개를 생성하고 그 중 같은 생일이 있는 그룹이 몇 개인지 헤아리는 시뮬레이션 Python Code

   1 import random
   2 
   3 class Birthday:
   4     def __init__(self, n=23):
   5         self.birthday = []
   6         for x in range(n):
   7             self.birthday.append(int(random.random()*365))
   8     def shared(self):
   9         seen = []
  10         for value in self.birthday:
  11             if value not in seen:
  12                 seen.append(value)
  13             else:
  14                 return True
  15         return False
  16 
  17 total = 10000
  18 shared = 0
  19 
  20 for x in range(total):
  21     birthday = Birthday()
  22     if birthday.shared():
  23         shared = shared +1
  24 
  25 print shared, "groups out of", total, "have members sharing the same birthday."

FunReading/BirthdayParadox (2016-03-13 10:20:21에 gehoon가(이) 마지막으로 수정)