Python生日悖论数学不起作用

时间:2014-10-03 02:41:11

标签: python-2.6

它运行corectly但它应该有大约500匹配,但它只有大约50,我不知道为什么! 这是我的comsci课程的一个问题,我有这个问题 我们必须创建一个函数来检查列表中的重复我得到了那个部分,但后来我们不得不将它应用于生日悖论(更多信息在这里http://en.wikipedia.org/wiki/Birthday_problem)这就是我遇到问题因为我的老师说的总次数应该在500或50%左右,但对我来说,它只能达到50-70倍或5%

duplicateNumber=0
import random 
def has_duplicates(listToCheck):
    for i in listToCheck:
        x=listToCheck.index(i)
    del listToCheck[x]
    if i in listToCheck:
        return True
    else:
        return False             
listA=[1,2,3,4]
listB=[1,2,3,1]
#print has_duplicates(listA)
#print has_duplicates(listB) 
for i in range(0,1000):
birthdayList=[]
    for i in range(0,23):  
        birthday=random.randint(1,365)
        birthdayList.append(birthday)
x= has_duplicates(birthdayList)
if x==True:
    duplicateNumber+=1 
else:
    pass
print "after 1000 simulations with 23 students there were", duplicateNumber,"simulations with atleast one match. The approximate probibilatiy is", round(((duplicateNumber/1000)*100),3),"%"

3 个答案:

答案 0 :(得分:0)

这是您正在寻找的。由于这不是标准做法(只是向新用户抛出代码),如果这会冒犯任何其他用户,我深表歉意。但是,我相信如果用户在他的职业生涯中进一步缺乏文档,那么显示OP正确的编写程序的方式应该可以帮助我们。

因此,请仔细查看代码,并填写空白。查看python doumentation(尽管它是干的),并尝试理解你不能立即得到的东西。即使您只是通过名称理解某些内容,在使用某种内置方法时仍然明智地看到实际发生的事情。

最后但并非最不重要的是,请查看此代码,并查看您的代码。注意差异,并继续尝试从头开始编写代码(不看我的代码),如果它搞砸了,看看你哪里出错了,重新开始。如果您希望稍后在编程中取得成功,这种做法是关键!

def same_birthdays():
    import random
    '''
    This is a program that does ________. It is really important
    that we tell readers of this code what it does, so that the
    reader doesn't have to piece all of the puzzles together,
    while the key is right there, in the mind of the programmer.
    '''
    count       =   0
    #Count is going to store the number of times that we have the same birthdays
    timesToRun  =   1000 #timesToRun should probably be in a parameter
    #timesToRun is clearly defined in its name as well. Further elaboration
    #on its purpose is not necessary.
    for i in range(0,timesToRun):
        birthdayList    =   []
        for j in range(0,23):
            random_birthday     =   random.randint(1,365)
            birthdayList.append(random_birthday)
        birthdayList = sorted(birthdayList) #sorting for easier matching
        #If we really want to, we could provide a check in the above nester
        #for loop to check right away if there is a duplicate.
        #But again, we are here
        for j in range(0, len(birthdayList)-1):
            if (birthdayList[j] == birthdayList[j+1]):
                count+=1
                break #leaving this nested for-loop
    return count

如果你想找到百分比,那么去掉上面的return语句并添加:

    return (count/timesToRun)

答案 1 :(得分:0)

这段代码给了我一个符合你期望的结果:

import random

duplicateNumber=0

def has_duplicates(listToCheck):
    number_set = set(listToCheck)

    if len(number_set) is not len(listToCheck):
        return True
    else:
        return False 

for i in range(0,1000):
    birthdayList=[]

    for j in range(0,23):  
        birthday=random.randint(1,365)
        birthdayList.append(birthday)

    x = has_duplicates(birthdayList)

    if x==True:
        duplicateNumber+=1

print "after 1000 simulations with 23 students there were", duplicateNumber,"simulations with atleast one match. The approximate probibilatiy is", round(((duplicateNumber/1000.0)*100),3),"%"

我做的第一个更改是整理你在那些嵌套for循环中使用的索引。您会看到我将第二个更改为j,因为它们之前是机器人i

但最重要的是has_duplicates功能。这里的基本原则是从传入列表中创建set获取列表中的唯一值。通过将number_set中的项目数与listToCheck中的数字进行比较,我们可以判断是否存在重复项。

答案 2 :(得分:0)

这是一个不使用set()的解决方案。它还对数组采用不同的方法,以便每个索引代表一年中的某一天。我还删除了hasDuplicate()函数。

import random 

sim_total=0
birthdayList=[]

#initialize an array of 0's representing each calendar day
for i in range(365):
    birthdayList.append(0)

for i in range(0,1000):
  first_dup=True 
  for n in range(365):
    birthdayList[n]=0
  for b in range(0, 23):
    r = random.randint(0,364)
    birthdayList[r]+=1
    if (birthdayList[r] > 1) and (first_dup==True):
      sim_total+=1
      first_dup=False

avg = float(sim_total) / 1000 * 100

print "after 1000 simulations with 23 students there were", sim_total,"simulations with atleast one duplicate. The approximate problibility is", round(avg,3),"%"