I love things like this. Earlier, we produced a working example of the Monte Hall problem. In this post, we show something similar for the birthday paradox…
c:\Python27>type c:\Users\showard\bday.py from random import randint cnt = 0 for k in range(2000): persons = dict() for i in range(23): persons[i] = randint(1,365) b = False for i in persons: for j in persons: if persons[j] == persons[i] and i != j: cnt += 1 b = True break if b == True: break print cnt * 100 / 2000 c:\Python27>python.exe c:\Users\showard\bday.py 49 c:\Python27>
As you can see, we find a match 49% of the time in which at least one pair of persons shares the same birthday in the year, even though we only used 23 samples in each run. This will vary right around 50% for 23 samples.
Another way of doing this is by taking the product of each probability, as shown in the wikipedia link above.
>>> total = 1 >>> for i in range(365,342,-1): ... total = total * (float(i) / 365) ... print round(total,2),i ... 1.0 365 1.0 364 0.99 363 0.98 362 0.97 361 0.96 360 0.94 359 0.93 358 0.91 357 0.88 356 0.86 355 0.83 354 0.81 353 0.78 352 0.75 351 0.72 350 0.68 349 0.65 348 0.62 347 0.59 346 0.56 345 0.52 344 0.49 343 >>>