0

I have a question where it shows customers entering a bar at a rate of 7 per hour, the question states i NEED to use random.expovariate() and generate a histogram showing 100 inter-arrival times. So far i have this

import numpy as np
from matplotlib import pyplot as plt
import random

def customers():    
    x=np.random.expovariate(7,100)                                                                                                                                
    plt.hist(x,100)
    plt.axis([-0,100,0,100])
    plt.show()
    return True

def main():
    global history
    print(customers()) 

if __name__ == "__main__":
    main()

And I am getting the error

AttributeError: 'module' object has no attribute 'expovariate'

Also i am not 100% this is how i would show the graph, i have researched it and not found a clear answer anywhere! Hope you can help

4

1 回答 1

1

这里有一些让你开始的东西:

import numpy as np
from matplotlib import pyplot as plt
import random

def customers():
    x = [random.expovariate(7) for r in xrange(100)]                   
    plt.hist(x,10)
    plt.show()
    return True

def main():
    global history
    print(customers()) 

if __name__ == "__main__":
    main()

你的主要问题是:

  1. 你打电话np.random.expovariate()而不是random.expovariate()

  2. random.expovariate()接受 1 个参数而不是 2 个

  3. random.expovariate()只产生一个数字,要创建一个直方图,您需要多个数字。在上面我创建了一个显式随机数列表。

于 2013-04-04T12:30:57.160 回答