Home > other >  Python birthday paradox urgent request
Python birthday paradox urgent request

Time:04-26

Birthday paradox refers to a room if there are 23 people or more, then at least two personal birthday probability is greater than 50%, the same program, the output 10000 cases in random sample quantity, 23 people in at least two personal birthday the same probability,
Import randombirth=month=day=[] [] [] x=0 for j in range (1000) : for I in range (23) : the month. The append (random. The randint (1, 12)) # 1,3,5,7,8,10,12 if the month in,3,5,7,8,10,12 [1] : [I] day.to append (random. The randint (1, 31)) if the month in,6,9,11 [4] : [I] day.to append (random. The randint (30) 1) if the month [I]==2: day.to append (random. The randint (1, 29) birth. Append ((the month [I], day [I])) if len (set) (birth) & lt; Len (birth) : x=x + 1 print (" under the random sample, 23 people in at least two personal birthday the same probability is: {} ". The format (1000)/x)
Don't know where the problem is, the output 0.999 or 1

CodePudding user response:

Birth=[]
The month=[]
Day=[]
The three sentences should be under the J loop, because each loop is to regenerate the 23 birthday, the three lists should be empty,

CodePudding user response:

Oh, yes, thank you
  • Related