最好(最快)的方法是什么?
这产生了我认为正确的答案,但显然在 N = 10e6 时它非常缓慢。我想我需要保持 Xi 值,这样我才能正确计算标准偏差,但是有什么技术可以让这个运行得更快吗?
def randomInterval(a,b):
r = ((b-a)*float(random.random(1)) + a)
return r
N = 10e6
Sum = 0
x = []
for sample in range(0,int(N)):
n = randomInterval(-5.,5.)
while n == 5.0:
n = randomInterval(-5.,5.) # since X is [-5,5)
Sum += n
x = np.append(x, n)
A = Sum/N
for sample in range(0,int(N)):
summation = (x[sample] - A)**2.0
standard_deviation = np.sqrt((1./N)*summation)