-1

I tried to write a function of 'The Birthday Paradox'. I found some examples in the internet and succseed in combining everything together with some modification but still there are things that I don't understand in my program.

This is my program:

# The Birthday Paradox

print "If there are 23 students in your class, what are the chances that another student and you will have birthday in the same day?"
print ""
print "Use the function has_duplicates(numExamples) to check it out!"
print "Please write in (numExamples) the number of examples of different lists of birthdays of 23-students-classes you want to check."

def has_duplicates(numExamples):

import random
probability = float()

for example in range(numExamples):
   year = [0]*365
   print year
   foundprobability = False
   for i in range(23):
       birthday = random.randrange(365)
       year[birthday] = year[birthday] + 1
       if year[birthday]>1:
          foundprobability = True

   if foundprobability == True:
       probability = probability + 1

countprobabilty = float(probability/numExamples)
print "The probability of a shared birthday in", numExamples,
print "examples of different lists of birthdays of 23-students-classes is", countprobabilty

I dont understand what this line means:

year = [0]*365

why would I want such a list [0,0,0,0...] ?

Thank you! Netta, the biology student

smci
  • 32,567
  • 20
  • 113
  • 146
Netta
  • 63
  • 8
  • 2
    `year = [0]*365` creates a list with 365 elements of the value 0: `[0,0,0,0...]` – Oisin Jan 15 '16 at 14:32
  • I know, but why to create such a list? – Netta Jan 15 '16 at 14:34
  • Possible duplicate of [Initializing a list to a known number of elements in Python](http://stackoverflow.com/questions/521674/initializing-a-list-to-a-known-number-of-elements-in-python) – rbp Jan 15 '16 at 14:35
  • You're not actually saying the birthday paradox correctly. Your comment says "If there are 23 students in your class, what are the chances that another student and you will have birthday in the same day?"; the actual phrasing should be something like If there are 23 students in the class, what are the probability that at least 2 of them have the same birthday – Foon Jan 15 '16 at 14:40
  • Its also unclear if he is asking about Python the statements `year=` and `birthday=`, or about how the birthday paradox itself works. – rbp Jan 15 '16 at 14:42
  • In a class of 23, the probability of someone sharing *your* birthday is less than 6%. The probability that *some* pair will share is close to 50-50. – Lee Daniel Crocker Jan 15 '16 at 20:40
  • Possible duplicate of [Create list of single item repeated N times](https://stackoverflow.com/questions/3459098/create-list-of-single-item-repeated-n-times) – smci Oct 10 '19 at 03:10

2 Answers2

1

year = [0]*365 creates a list with 365 elements of the value 0: [0,0,0,0...]

birthday = random.randrange(365) creates a list from 0 to 365 and chooses a random element.
See https://docs.python.org/2/library/random.html#random.randrange

Oisin
  • 770
  • 8
  • 22
1

because the algorithm requires a counter for each day of the 365-day year, as follows:

   year[birthday] = year[birthday] + 1

In order for year[birthday] to appear in this statement, it has to have been previously initialized, and

    year = [0] * 365

initializes it.

rbp
  • 1,850
  • 15
  • 28