我试图解决从书中探索python的二维随机游走问题。 但是,我无法弄清楚我该如何解决这个问题。我做了一些研究,但是这些太复杂了,无法理解它是什么。我是初学者。所以,我无法通过查看来理解代码。请详细解释我这个问题。另一个简单的随机游走模拟使用Python(二维)
无论如何,问题是:
上随机游走二维变化在网格的中间 开始,例如11 11阵列。在每一步醉酒都有四个选择:上,下,左或右。在本章前面我们描述了 如何创建一个二维数组数组。使用这个数据 类型,写一个二维随机游走的模拟。
好的,我所知道的; 我知道如何在Python创建二维数组:
times = [0] * 11
for i in range(0,11):
times[i] = [0] * 11
而且我得到了“randint”功能的想法:
而且也是我最近写了这个问题的一个尺寸变化。但它是一个意大利面代码,而且它非常脏,我也不确定它是否正确。
我的代码是在这里:
'''
Created on Feb 11, 2012
@author: msarialp
'''
from random import randint
def drunken_man():
steps = 0
times = [0] * 11
left_move = 0
right_move = 0
i = 0
while left_move < 5 or right_move < 5:
value = randint(0,1)
times[5] = 1
if value == 1:
steps += 1
print("He moved left")
left_move += 1
if right_move > 0:
right_move -= 1
if left_move == 1:
times[4] += 1
elif left_move == 2:
times[3] += 1
elif left_move == 3:
times[2] += 1
elif left_move == 4:
times[1] += 1
#elif left_move == 5:
#times[0] += 1
elif value == 0:
steps += 1
print("He moved right")
right_move += 1
if left_move > 0:
left_move -= 1
if right_move == 1:
times[6] += 1
elif right_move == 2:
times[7] += 1
elif right_move == 3:
times[8] += 1
elif right_move == 4:
times[9] += 1
#elif right_move == 5:
#times[10] += 1
times[i] += 1
for i in range(1,10):
print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1} square at {0} times".format(times[i], i))
def main():
drunken_man()
return 0
if __name__ == '__main__':
main()
编辑一个
从丹Gerhardsson采取了一些很好的建议后。 我决定编辑我的问题。 所以,我在这个问题上: 我明白我该如何遵循和检查我的醉酒男子在两个步骤的步骤。
使用元组来解决这个练习是非常容易理解和清楚的。
因此,毕竟我的代码段在这里,请检查并给我任何反馈。
def two_dimensional_random_walk():
steps = 0
times = [0] * 11
for i in range(0,11):
times[i] = [0] * 11
x = 5
y = 5
moves = [(1,0), (0,1), (-1,0), (0,-1)]
while x<11 and x >= 0 or y < 11 and y >= 0:
dx, dy = moves[randint(0,3)]
x += dx
y += dy
if dx == 1 and dy == 0:
print("He moved right")
elif dx == 0 and dy == 1:
print("He moved up")
elif dx == -1 and dy == 0:
print("He moved left")
elif dx == 0 and dy == -1:
print("He moved down")
try:
times[x][y] += 1
steps += 1
except IndexError:
break
我的打印功能:
for i in range(0,11):
for j in range(0,11):
print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1}x{2} square at {0} times".format(times[i][j], i+1,j+1))
因此,所有的一切我想与帮助丹Gerhardsson,我解决了这个运动。
但是,为什么我不能用这些提示来改变我的一维解决方案。
def drunken_man():
steps = 0
x = 6
times = [0] * 11
moves = [(1), (-1)]
while x < 11 and x >= 0:
dx = moves[randint(0,1)]
print(dx, x)
x += dx
try:
times[x] += 1
steps += 1
except IndexError:
break
for i in range(1,11):
print("He took {0} steps until he reaches end of the sidewalk.".format(steps), "He stood on {1} square at {0} times".format(times[i], i))
编辑两个(最后的润色)
我不知道是否有必要编辑自己的帖子由丹Gerhardsson应用提示。为了帮助那些错过了像我这样的观点的人,我决定把所有东西结合在一起。
因此,这里是我的功能与丹Gerhardsson提示合并:
def two_dimensional_random_walk():
steps = 0 # Steps counter for understand how many steps that our drunken man take
grid_size = 11 # Grid size variable,
# Creating Two dimensional array by using lists
times = [0] * grid_size
for i in range(0,grid_size):
times[i] = [0] * grid_size
# Initial variables to start in the middle of grid
x = 5
y = 5
# Tuples to get directions and decide where to go
moves = [(1,0, "right"), (0,1, "up"), (-1,0, "left"), (0,-1, "down")]
# My loop for evaluate the steps
while True:
dx, dy, position = moves[randint(0,3)] # By using randint I could make decision randomly
x += dx
y += dy
print("He moved", position)
try:
times[x][y] += 1 # And here is, how many times have he stood on each square
steps += 1
except IndexError: # The exit of loop
break
# My print function which answers these questions (How long will it be until he reaeches the end of the sidewalk, and how many times will he have stood on each square)
for i in range(0,11):
for j in range(0,11):
print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1}x{2} square at {0} times".format(times[i][j], i+1,j+1))
感谢您的帮助很大丹Gerhardsson。 我想我终于得到了解决方案。
这功课吗? – amindfv 2012-02-23 21:59:40
不,我只是在学习Python这本书,http://books.google.com.tr/books/about/Exploring_Python.html?id=VnAsHwAACAAJ&redir_esc=y – mustafaSarialp 2012-02-23 22:03:52