Advertisement
snozzy

birth_day_problem

Feb 17th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. from random import randint
  2.  
  3.  
  4. def simulate(n):
  5.     simulations = n
  6.     birthdays = []
  7.     attempts = 0
  8.     while simulations > 0:
  9.         for _ in range(n):
  10.             day = randint(0, 365)
  11.             if day in birthdays:
  12.                 birthdays = []
  13.                 simulations -= 1
  14.             else:
  15.                 birthdays.append(day)
  16.                 attempts += 1
  17.     return attempts / n
  18.  
  19.  
  20. print("Det behövs i medelsnitt", simulate(100),
  21.       "elever innan två har samma födelsedag")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement