Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. '''
  4.  
  5. Testing the validity of this statement:
  6.  
  7. "In a room of 23 people, there's a better-than-50%
  8. chance that two people have the same birthday"
  9.  
  10. This script creates 23 random birthdays and tests for dupes, 1,000,000 times.
  11.  
  12. Turns out it's true! Always outputs something in the range of 50.7%.
  13.  
  14. '''
  15.  
  16. from random import randrange
  17. from collections import Counter
  18.  
  19. # how many times to run the experiment?
  20. total_sessions = 1000000
  21. sessions = []
  22. for i in range(total_sessions):
  23.  
  24. # create 23 random birthdays
  25. all_bdays = []
  26. for i in range(23):
  27. bday = randrange(0, 365)
  28. all_bdays.append(bday)
  29.  
  30. # check for dupes
  31. if len(all_bdays) != len(set(all_bdays)): dupes_exist=True
  32. else: dupes_exist=False
  33. sessions.append(dupes_exist)
  34.  
  35. c = Counter(sessions)
  36. print c
  37. percent_dupes = (c[True] / float(total_sessions)) * 100
  38. print percent_dupes
  39.  
  40. # Always prints around 50.7208
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement