Created
August 11, 2008 17:35
-
-
Save cannikin/4899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import random, datetime, decimal | |
count = 0 | |
maxBDays = int(raw_input("Number of people to put into random group? ")) | |
maxGroups= int(raw_input("Number of times to run the experiment? ")) | |
for x in range(maxGroups): | |
bdays = [] | |
for m in range(maxBDays): | |
bdays.append(datetime.date.today() + datetime.timedelta(days=random.randint(0,365))) | |
for b in bdays: | |
if bdays.count(b) == 2: | |
count = count + 1 | |
break | |
print "Percentage of groups with at least two people having same birthday: %d%%" % (decimal.Decimal(count) / decimal.Decimal(maxGroups) * 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# This app proves the Birthday Paradox: http://en.wikipedia.org/wiki/Birthday_problem | |
# | |
# The Birthday Paradox is counter-intuitive to common sense - select 23 people at random and | |
# there is a greater than 50% chance that at least two people in the group will have the same | |
# birthday. With 50 random people the probability goes up to 97%. These solutions ignore leap-years. | |
# | |
# People Probability | |
# --------------------- | |
# 10 11.7% | |
# 20 41.1% | |
# 23 50.7% | |
# 30 70.6% | |
# 50 97.0% | |
# 57 99.0% | |
# 100 99.99997% | |
# 200 99.9999999999999999999999999998% | |
# 300 (100 − (6×10^−80))% | |
# 350 (100 − (3×10^−129))% | |
# 366 100% | |
# | |
# This code available at http://gist.github.com/4899 | |
require 'date' | |
# extend the Array class to include a `count` method - accepts a value and returns the number of times that value appears in the string | |
class Array | |
def count(what) | |
return self.select { |value| value == what }.length | |
end | |
end | |
# creates a group of people with random birthdays | |
class Group | |
def self.random(num=10) | |
people = [] | |
1.upto(num) do |i| | |
people << Date.today + rand(366) | |
end | |
return people | |
end | |
end | |
# gets a group of people and finds out if at least two people in that group have the same birthday | |
class Birthday | |
def self.paradox(people_per_group=10,number_of_groups=1) | |
results = [] | |
1.upto(number_of_groups) do |i| | |
people = Group.random(people_per_group) | |
results << (people == people.uniq) # compare the array of people to an array with duplicates removed - if they're different at least two people have the same birthday | |
end | |
return results | |
end | |
end | |
# get input from user | |
print 'Number of people to put into random group? ' | |
people_per_group = gets.chomp.to_i | |
print 'Number of times to run the experiment? ' | |
number_of_groups = gets.chomp.to_i | |
puts 'Running...' | |
results = Birthday.paradox(people_per_group,number_of_groups) | |
puts "Percentage of groups with at least two people having same birthday: #{results.count(false).to_f / number_of_groups * 100}%" | |
# sample output | |
######################################### | |
# Number of people to put into random group? 23 | |
# Number of times to run the experiment? 1000 | |
# Running... | |
# Percentage of groups with at least two people having same birthday: 49.3% | |
######################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment