Skip to content

Instantly share code, notes, and snippets.

@akshaykarnawat
Created June 30, 2017 03:09
Show Gist options
  • Save akshaykarnawat/fd18dcbec87e0ff0815f1afd48b606ea to your computer and use it in GitHub Desktop.
Save akshaykarnawat/fd18dcbec87e0ff0815f1afd48b606ea to your computer and use it in GitHub Desktop.
Finds the probability of two people sharing the same birthday with the given number of people in the room
import pylab
import numpy as np
# The function finds the probability of two people have the same
# birthday with the given number of people in the room
def findProbability(n):
x = [] # the x range of values to plot
y = [] # the y range of values to plot
p = 1 # the probability
# find the probability given n numbers of people in the room
for i in range(n):
# multiply by itself to get the factorial effect
p *= float((float((365-i))/365))
x.append(i) # add x values to the list
y.append(1-p) # add y values to the list
# plot the line graph values
pylab.plot(x,y,linestyle='-',marker='o')
pylab.show()
# run the program
#findProbability(15)
#findProbability(23)
#findProbability(32)
#findProbability(41)
findProbability(350)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment