【发布时间】:2014-10-16 02:21:01
【问题描述】:
我正在模拟二维随机游走,方向 0
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
【问题讨论】:
-
重复上述 12 次,围绕你已有的内容的 for 循环会很好地工作。
-
在发布问题之前,您应该尝试自己解决更多问题。你说,“我需要一个while循环吗..”,最好的答案是尝试让它与
while循环一起工作。另外,尝试使用for循环使其工作,因为您也知道这一点。另存为文本?,谷歌“numpy另存为文本”,看看你得到了什么..第一个命中是numpy.savetxt,第二个是numpy input and output。 -
@tom10 我在帖子中添加了一个猜测。我在正确的路线上吗?
标签: python numpy random-walk