1

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:

  1. the list has just one item

enter image description here

  1. the items in the list are equal

enter image description here

  1. or the numRolls are limited to one roll

enter image description here

This is how it should look like

enter image description here

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

Mickey Mahoney
  • 361
  • 1
  • 4
  • 15
  • Can you share the command you are using to generate your histograms? – nbryans Dec 20 '16 at 01:08
  • You have not given us enough information. Just how are you getting those histograms? See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Rory Daulton Dec 20 '16 at 01:09
  • "Consecutive" is with a 'c', not a 'g' ;-) – thebjorn Dec 20 '16 at 01:12
  • 2
    Why are you even trying to make histograms for one value or one average value? A quick if statement to check for that and just display the value instead of a histogram - where a single bar for a single bin would be correct - might be a better approach. – LinkBerest Dec 20 '16 at 01:20
  • Problem with calling the method (which was not your original question) is covered in [this question](http://stackoverflow.com/questions/5615648/python-call-function-within-class) - still wondering what the problem with the histograms is. – LinkBerest Dec 20 '16 at 02:14

1 Answers1

0

def getAverage(die, numRolls, numTrials): """ - 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 """ 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: count = 1 if count > tempCount: tempCount = count if count > tempCount: tempCount = count res.append(tempCount) mean, std = getMeanAndStd(res) makeHistogram(res, 10, 'Quantity of Consecutive Numbers', 'Consecutive Number per Run') return round(mean, 3)