-1

所以基本上我需要创建一个标记为 [0,1,2,3,4,5,10,25,100] 的缓冲区列表。然后我需要在 (0.75 和 4.75)+3 之间创建随机数。现在我需要打印类似“Buffer 3 take and average of x seconds”之类的东西。x将是生成的随机数。每个缓冲区必须有自己的随机数

def main():
    buffer= [0,1,2,3,4,5,10,25,100]
    time= random.uniform(0.75,4.75)+3
    sum=0
    for i in range (1, 500):
        sum = sum + time;
        average= sum/1500
        for buffer in range(0,1,2,3,4,5,10,25,100):
           print("For Buffer",buffer,",the average time is",average,".")

[解决了]

#Lab 1 using python.
Import random  #Importing random number generator.
def main():
    buffers= [0,1,2,3,4,5,10,25,100]; #Defining the buffers using list.
    for buffer in buffers:
        sum=0;

        for i in range (1, 500): # Create a loop which will create random number and increment 500 times.
            time= (random.uniform (0.75,4.75))+3
            sum = sum + time

        average= sum/500; #Finding the average of each buffers random numbers.

        print("For Buffer",buffer,",the average time is",average,"."); #Print the output.
4

2 回答 2

1

您只需要更改它,以便您的平均计算在您的 for 循环中完成。

buffer= [0,1,2,3,4,5,10,25,100]
    for buffer in range(0,1,2,3,4,5,10,25,100):
          sum=0
          for i in range (1, 500):
             time= random.uniform(0.75,4.75)+3
             sum = sum + time;
             average= sum/1500
          print("For Buffer",buffer,",the average time is",average,".")
于 2013-02-26T15:19:33.917 回答
0

您可以使用字典将每个缓冲区与其平均值相关联,该字典将缓冲区直接用作键,并将其平均值作为值。

def main():
    buffer= [0,1,2,3,4,5,10,25,100]
    time= random.uniform(0.75,4.75)+3
    sum=0
    dict_of_buffers = {}
    for i in range (1, 500):
        sum = sum + time;
        average= sum/1500
        for buffer in range(0,1,2,3,4,5,10,25,100):
            print("For Buffer",buffer,",the average time is",average,".")
            dict_of_buffers[buffer] = average
于 2013-02-26T15:20:22.687 回答