""" Made late on the evening of April 15, 2015 by @cricketingview on twitter Plays a match of n overs length, m times. Each team has an assigned strength relative to the other - astr and bstr Each of the n overs can be won by one of the two sides. In each match, if the number of overs won by each side is equal, the game is a tie. """ import collections, random def play(m, n,astr,bstr): z = collections.defaultdict(int) overs = n ties,awins,bwins = 0,0,0 alla, allb = 0,0 AStr = range(astr)#The range of quality for players in A BStr = range(bstr) #The range of quality for players in B #Play m matches for i in range(m): alla, allb = 0,0 #Play each match over n overs while n > 0: a = random.random()*random.choice(AStr) #returns a number between 0 and 1 b = random.random()*random.choice(BStr) #Decide winner for the current over if a > b: alla += 1 elif a < b: allb += 1 n = n - 1 #Check if the match is a tie if alla == allb: ties += 1 elif alla < allb: bwins += 1 elif alla > allb: awins += 1 n = overs r0 = str(round(100.0*ties/m,2)) + ' % Ties in '+ str(m) + ' games' r1 = str(round(100.0*awins/m,2)) + ' % wins for stronger team in '+ str(m) + ' games' r2 = str(round(100.0*bwins/m,2)) + ' % wins for weaker team in '+ str(m) + ' games' strength = "in which strength of stronger to weaker is "+str(max(astr,bstr))+":"+str(min(astr,bstr)) matchlength = "In a contest which is "+str(n)+" overs long" result = matchlength + ', ' + strength + '\n' + r0 + ', ' + r1 + ', ' + r2 return result """ If you want to run this 10000 or more times, copy this code into a file called 'filename.py', install python if you don't have it, and run it locally. It does 10000 less than 1.5 seconds on my machine. """ """ Just for fun, if the IPL wants to make things \ even more exciting by playing 10 overs a side play a 10 overs a side game too. :) """ print play(500,20,9,8) print '******************************************************' print play(500, 40,9,8) print '*******************************************************' print play(500, 100,9,8) print '*******************************************************' print play(500, 450,9,8) #This might take a while. print '*******************************************************' print 'These results can be considered quasi-seriously \ only in comparison to each other.'
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