2012-06-23 65 views
0

我正在通過歐拉項目解決我的問題。我已經達到question 84,我創建了一個遊戲模擬。我針對問題中提供的統計信息運行了模擬,並獲得了正確的序列。當我嘗試用2d4運行時,我得到錯誤的結果。通常我得到101516.我錯過了什麼?我對歐拉項目的方法p84錯過了什麼?

請注意,我不是在尋找解決方案或爲您解決我的代碼。我只想知道我的算法在哪裏存在缺陷。

from random import randint 
import sys 

pos = 0 
doubles = 0 

csqs = [2,17,33] #The cc position 
hsqs = [7,22,36] #The ch positions 
rounds = 0 
stop = 100000 
sqs = dict() #will store how many visit I had on each square 


for i in range (0,40): #initial values of sqs 
    sqs[i] = 0 

def doCC():# the cc cards. There is no real effect of randomly picking a card or kipping them in order on my result 
    global pos,doubles 
    global CC 
    if CC == 0: 
     pos = 0 
    elif CC == 1: 
     pos = 10 
     doubles = 0 
    CC += 1 
    if CC == 16: CC = 0 

def doCH(): #CH cards 
    global pos, doubles, CH 
    if CH == 0: pos = 0 
    elif CH == 1: 
     pos = 10 
     doubles = 0 
    elif CH == 2: pos = 11 
    elif CH == 3: pos = 24 
    elif CH == 4: pos = 39 
    elif CH == 5: pos = 5 
    elif CH == 6 or CH == 7: 
     if pos == 7: 
      pos = 15 
     elif pos == 22: 
      pos = 25 
     elif pos == 36: 
      pos = 5 
    elif CH ==8: 
      if pos == 22: pos = 28 
      else: pos = 12 
    elif CH == 9: 
      pos -= 3 
    CH += 1 
    if CH == 16: CH = 0 

while rounds < stop: 
    d1 = randint(1,4) 
    d2 = randint(1,4) 
    if d1 == d2: 
     doubles += 1 #counting doubles 
    else: 
     doubles = 0 
    if doubles == 3: 
     pos = 10 
     doubles = 0 
    pos += d1+d2 
    if pos>= 40 : #you have just crossed go 
     pos -= 40 
     rounds += 1 
     if rounds %10000 == 0: print rounds, 
    if pos == 30: #g2j 
     doubles = 0 
     pos = 10 
    if pos in hsqs: 
     doCH() 
    if pos in csqs: 
     doCC() 
    sqs[pos] += 1 
    sys.stdout.flush() 
# Setting values 
m1 = 0 
m2 = 0 
m3 = 0 
v1 = 0 
v2 = 0 
v3 = 0 


su = 0 
for v in sqs: 
    m = sqs[v] 
    su += m 
    if m > m1: 
     m1 = m 
     v1 = v 

for v in sqs: 
    m = sqs[v] 
    if m > m2 and m < m1: 
     m2 = m 
     v2 = v 

for v in sqs: 
    m = sqs[v] 
    if m > m3 and m < m2: 
     m3 = m 
     v3 = v 

for v in sqs: 
    sqs[v] = sqs[v]*100.0/su 

print m1,m2,m3 
print v1,v2,v3 
print m1*100.0/su, m2*100.0/su, m3*100.0/su 

print sqs 

回答

1

當你有一排三項雙打,

if doubles == 3: 
    pos = 10 
    doubles = 0 
pos += d1+d2 

你還是跳了監獄,但你不應該。