I wrote a function aveMean(die, numRolls, numTrials) which requires the following:
Blockquote
- die, a Die
- numRolls, numTrials, are positive ints
- Calculates the expected mean value of the longest run of a number over numTrials runs of numRolls rolls.
- Calls makeHistogram to produce a histogram of the longest runs for all the trials. There should be 10 bins in the histogram
- Choose appropriate labels for the x and y axes.
- Returns the mean calculated
Blockquote
Everything works fine, except:
- the list has just one item
- the items in the list are equal
- or the numRolls are limited to one roll
This is how it should look like
You see that in 1.-3. there is always a little extra bar (barely visible) on the left side. How can I get rid of it? I read something about this is due to Python 3, but I didn't find out a solution.
Thanks!
P.S. I edited the code how to call the histogram and also the function that uses the call to create the histogram:
def getMeanAndStd(X):
mean = sum(X)/float(len(X))
tot = 0.0
for x in X:
tot += (x - mean)**2
std = (tot/len(X))**0.5
return mean, std
class Die(object):
def __init__(self, valList):
""" valList is not empty """
self.possibleVals = valList[:]
def roll(self):
return random.choice(self.possibleVals)
def makeHistogram(values, numBins, xLabel, yLabel, title=None):
pylab.hist(values, numBins)
pylab.xlabel(xLabel)
pylab.ylabel(yLabel)
if(title != None): pylab.title(title)
pylab.show()
def aveMean(die, numRolls, numTrials):
tries = die
testList, res = [], []
for i in range(numTrials):
count, tempCount, testList = 1, 1, []
for i in range(numRolls):
testList.append(tries.roll())
for i in range(1, numRolls):
if testList[i-1] == testList[i]:
count +=1
else:
if count > tempCount:
tempCount = count
count = 1
else:
count = 1
res.append(tempCount)
mean, std = getMeanAndStd(res)
makeHistogram(res, 10, 'Quantity of Consecutive Numbers', 'Consecutive Number per Run')
return round(mean, 3)
The Error Message I get is: Unsuccessfully called makeHistogram