Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- '''
- Testing the validity of this statement:
- "In a room of 23 people, there's a better-than-50%
- chance that two people have the same birthday"
- This script creates 23 random birthdays and tests for dupes, 1,000,000 times.
- Turns out it's true! Always outputs something in the range of 50.7%.
- '''
- from random import randrange
- from collections import Counter
- # how many times to run the experiment?
- total_sessions = 1000000
- sessions = []
- for i in range(total_sessions):
- # create 23 random birthdays
- all_bdays = []
- for i in range(23):
- bday = randrange(0, 365)
- all_bdays.append(bday)
- # check for dupes
- if len(all_bdays) != len(set(all_bdays)): dupes_exist=True
- else: dupes_exist=False
- sessions.append(dupes_exist)
- c = Counter(sessions)
- print c
- percent_dupes = (c[True] / float(total_sessions)) * 100
- print percent_dupes
- # Always prints around 50.7208
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement