2D随机游走,python
我写这段代码有困难。二维随机游走。二维随机行走模拟粒子在点栅格中移动的行为。在每一步中,随机步行者以1/4的概率向北、南、东或西移动,与之前的移动无关。编写一个程序,该程序使用命令行参数n,并估计随机漫游器到达以起点为中心的2n+1×2n+1正方形的边界所需的时间 这就是我写的所有代码2D随机游走,python,python,arrays,numpy,python-requests,2d,Python,Arrays,Numpy,Python Requests,2d,我写这段代码有困难。二维随机游走。二维随机行走模拟粒子在点栅格中移动的行为。在每一步中,随机步行者以1/4的概率向北、南、东或西移动,与之前的移动无关。编写一个程序,该程序使用命令行参数n,并估计随机漫游器到达以起点为中心的2n+1×2n+1正方形的边界所需的时间 这就是我写的所有代码 import sys import stdio import random # the size of the box, which should be 4 n = in
import sys
import stdio
import random
# the size of the box, which should be 4
n = int(sys.argv[1])
# should be the amount of steps taken to reach the boundary/ end of the box.
c = int(sys.argv[2])
def random_walk_2D (n):
x, y = 0, 0
i = 4
North = 1; South = 2; West = 3; East = 4
for i in range(n):
people = random.randint(1,4)
while I < c:
stdio.write(
if people == 1:
y = y + 1
elif people == 2:
y = y - 1
elif people == 3:
x = x + 1
else :
x = x - 1
return (x, y)
sdio.write('The walker took ')
stdio.write(c)
stdio.write(' steps. ')
导入系统
进口stdio
随机输入
#盒子的大小,应该是4
n=int(sys.argv[1])
#应为到达箱子边界/末端所采取的步骤数量。
c=int(sys.argv[2])
def随机行走2D(n):
x、 y=0,0
i=4
北=1;南=2;西=3;东=4
对于范围(n)中的i:
people=random.randint(1,4)
而Idef random\u walk\u 2d(n):
def random_walk_2d(n):
xy = 0+0j
cnt = 0
while(np.abs(np.real(xy)) < n or np.abs(np.imag(xy)) < n):
xy += np.random.choice([1j,-1j, 1, -1])
cnt += 1
return cnt
def simulate(N_sims,n):
expected_move = 0
for i in range(N_sims):
expected_move += random_walk_2d(n)
return expected_move/N_sims
xy=0+0j
cnt=0
而(np.abs(np.real(xy))
缩进有什么问题吗?我不理解你的问题如果你没有正确缩进代码,那么它就不会工作。