Skip to content Skip to sidebar Skip to footer

Is There A Reverse Way To Find Number Of People With Given 0.5 Probability That Two People Will Have Same Birthday But No Using Mathematical Formula?

I'm doing birthday paradox, and want to know how many people can meet 0.5 probability that two people have same birthday by using python. I have tried no using mathematical formula

Solution 1:

Well, problem with your code you check only consecutive birthday equality. Better check it using sets

Along the line

import random

defsim(n):
    """simulate birthdays for n people"""
    a = set([random.randint(1, 365) for _ inrange(n)])
    iflen(a) == n:
        returnFalsereturnTrueprint(sim(23))
print(sim(23))
print(sim(23))
print(sim(23))
print(sim(23))

Function above will return true if there are same day birthday for n people, false otherwise.

Call it 1000000 times for n = 20, 21, ...., 25 and count how many Trues vs Falses are there

Running code

nt = 0
nf = 0
n = 23
for k in range(0, 1000000):
    if sim(n):
        nt += 1
    else:
        nf += 1

print((nt, nf))

for n = 23 and n = 22 produced

(506245, 493755)
(475290, 524710)

Post a Comment for "Is There A Reverse Way To Find Number Of People With Given 0.5 Probability That Two People Will Have Same Birthday But No Using Mathematical Formula?"