import random def play_match(astr, bstr, d): """ Simulates 1 match lasting d deliveries. This is called in the simulate() function. """ A = range(astr) B = range(bstr) a_scr, b_scr = 0, 0 W, L, T = 0, 0, 0 n = 0 while n < d: a_scr = random.choice(A) b_scr = random.choice(B) if a_scr > b_scr: W += 1 elif b_scr > a_scr: L += 1 else: T += 1 n += 1 w, l, t = 0, 0, 0 if W > L: w = 1 elif W < L: l = 1 else: t = 1 return w, l, t def simulate(A, B, d, n): """ Calculates the win likelihoods of two teams depending on the length of the context. Teams A, B, have strength astr, bstr respectively. The match is played over d deliveries and simulated n times. """ w, l, t = 0, 0, 0 c = 0 while c < n: W, L, T = play_match(A, B, d) w += W l += L t += T c += 1 print "For two teams with strengths:", A,",", B,":", round(100.0*w/n, 1), round(100.0*l/n, 1), round(100.0*t/n, 1), "for", n, "simulations" def run(): """The first team is 1% stronger than the second""" simulate(100, 99, 40*6, 25000) simulate(100, 99, 100*6, 25000) simulate(100, 99, 450*6, 25000) run()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question