2014-10-03 50 views
1

它運行正常,但它應該有大約500場比賽,但它只有大約50,我不知道爲什麼! 這是我的comsci類的一個probelm,我與 我們不得不做一個函數,檢查列表重複我得到那部分,但然後我們不得不將它應用到生日悖論(更多信息在這裏http://en.wikipedia.org/wiki/Birthday_problem)這就是我在哪裏乳寧成問題,因爲我的老師說,總次數應在500或50%,但對我來說它只會50-70倍或5%Python生日悖論數學不起作用

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),"%" 
+0

只是站起來,下次您發佈信息時,您可能想嘗試讓您的帖子更多地瞭解您嘗試的內容,並清楚說明您遇到問題的位置。我不介意回答你的問題,但是我在這裏是百萬分之一。 – 2014-10-03 02:57:21

+0

這是我第一次使用這個網站,我會記住下次 – gamer232 2014-10-03 03:06:12

+0

考慮到你下面的評論,我覺得我將不得不改變我的答案。現在你已經編輯了你的文章,是**完全**,因爲它在你的腳本? – 2014-10-03 03:08:23

回答

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,因爲他們之前是bot i

儘管如此,大的功能是has_duplicates功能。這裏的基本原則是,在傳入列表中創建一個set將獲得列表中的唯一值。通過將number_set中的項目數與listToCheck中的數量進行比較,我們可以判斷是否有重複項。

+0

謝謝,幫助很多! – gamer232 2014-10-03 03:25:51

+0

還有一件事情,爲什麼要將它更改爲'j' – gamer232 2014-10-03 03:30:06

+0

@ gamer232在這種情況下,它不會引起任何問題,但它有助於使代碼更具可讀性。此外,在嵌套循環中通常希望從外部循環訪問索引值。通過爲兩者使用不同的變量名稱,您可以執行此操作。 – 2014-10-03 03:38:57

0

這裏是你在找什麼對於。由於這不是標準做法(只是向新用戶拋出代碼),如果這違反了其他任何用戶,我很抱歉。但是,如果用戶在職業生涯中缺乏進一步的文檔,我相信向OP提供正確的編寫程序的方式應該都可以幫助我們。

因此,請仔細查看代碼並填寫空格。查看蟒蛇運動(儘可能乾燥),並嘗試理解你不能馬上得到的東西。即使您只是通過名稱瞭解某些內容,但使用某種內置方法時,仍然可以明智地看到實際發生的情況。

最後但並非最不重要的,看看這段代碼,並看看你的代碼。注意不同之處,並不斷嘗試從頭開始編寫代碼(如果沒有看我的代碼),如果它搞砸了,看看你出錯的地方,然後重新開始。如果你希望在編程中獲得成功,這種做法是關鍵。

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) 
+0

它在我的權利,但我搞砸了在網站上的格式對不起 – gamer232 2014-10-03 03:05:09

+0

謝謝你的意見,我會盡你所能地謝謝你的幫助,我將永遠有更多的文件從現在開始 – gamer232 2014-10-03 03:35:55

+0

你是歡迎,請,不要擔心未來^^ – 2014-10-03 03:37:51

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),"%"