我正在模拟二维随机游走,方向 0 < θ < 2π 和 T=1000 步。我已经有了:
a=np.zeros((1000,2), dtype=np.float)
def randwalk(x,y):
theta=2*math.pi*rd.rand() # Theta is a random angle between 0 and 2pi
x+=math.cos(theta); # Since spatial unit = 1
y+=math.sin(theta); # Since spatial unit = 1
return (x,y)
x, y = 0., 0.
for i in range(1000):
x, y = randwalk(x,y)
a[i,:] = x, y
这会生成一次步行,并将所有中间坐标存储在 numpy 数组 a 中。如何编辑我的代码以重复步行 12 次(每次使用新的随机种子),然后将每次运行保存在单独的文本文件中?我的 randwalk 函数中是否需要一个 while 循环?
猜测:
rwalkrepeat = []
for _ in range(12):
a=np.zeros((1000,2), dtype=np.float)
x, y = 0., 0.
for i in range(1000):
x, y = randwalk(x,y)
a[i,:] = x, y
rwalkrepeat.append(a)
print rwalkrepeat