Geometric Distribution MIT

来源:互联网 发布:windows phone8支付宝 编辑:程序博客网 时间:2024/06/11 12:49
#quote from MIT 'introduction to computation and programming using python, Revised'import randomimport pylab    def successfulStarts(eventProb, numTrials):    """Assumes eventProb is a float representing a probability          of a single attempt being successful. numTrials a positive int       Returns a list of the number of attempts needed before a          success for each trial"""    triesBeforeSuccess = []    for t in range(numTrials):        consecFailures = 0        while random.random() > eventProb:            consecFailures += 1        triesBeforeSuccess.append(consecFailures)    return triesBeforeSuccess    random.seed(0)probOfSuccess = 0.5numTrials = 5000distribution = successfulStarts(probOfSuccess, numTrials)pylab.hist(distribution, bins=14)pylab.xlabel('Tries Before success')pylab.ylabel('Number of Occurence Out of ' + str(numTrials))pylab.title('Probability of Starting Each Try ' + str(probOfSuccess))pylab.show()

0 0
原创粉丝点击