Another simple random walk simulation using Python (two-dimensional)

I am trying to solve a two-dimensional random walk problem from a book by exploring python. But I could not understand how I can solve this problem. I did some research, but they were too complicated to understand what it was about. I am a beginner student. So, I can not understand the code by looking at it. Please explain this problem to me in detail.

In any case, the question is:

The two-dimensional variation on a random walk starts in the middle of the grid, such as an 11 by 11 array. At each stage, a drunk has four choices: up, down, left, or right. Earlier in the chapter, we described how to create a two-dimensional array of numbers. Using this type data, write a simulation of a two-dimensional random walk.

Good that I know; I know how to create a two-dimensional array in python:

times = [0] * 11 for i in range(0,11): times[i] = [0] * 11 

And I got the idea of ​​the randint function:

And also I recently wrote a one-dimensional variation of this problem. But this is the spaghetti code code, and it is very dirty, and I'm not sure if it is correct.

My code is here:

 ''' 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() 

EDIT One

After some good tips Dan Gerhardsson. I decided to change my question. So, where am I to this question: I understand how I can follow and explore the steps of my drunk man twice.

It was very clear and understandable to use a tuple to solve this exercise.

So, after all my code segment, please check and give me any feedback.

 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 

And my print function:

 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) ) 

So, all that I think with the help of Dan Gerhardson, I decided to exercise.

But why can't I change the one-dimensional solution with these tips.

 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) ) 

EDIT Two (finishing touches)

I'm not sure if my post needs to be edited to apply Dan Gerhardson's hints. To help someone who missed moments like me, I decided to bring everything together.

So, here is my function that combines with Dan Gerhardson's hints:

 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) ) 

Thanks for your great help to Dan Gerhardson. I think, finally, I have a solution.

+6
source share
2 answers

I can at least give you some advice. Thus, you have four possible moves. Each movement can be represented by a tuple, which is an offset in the x and y directions:

 moves = [(0, 1), (1, 0), (0, -1), (-1, 0)] 

To set the starting position in the center:

 grid_size = 11 x = grid_size // 2 y = grid_size // 2 

Keep the position of the drunk person and update him at each stage of the simulation. Something like that:

 # Displacement: dx, dy = random.choice(moves) # Update position: x += dx y += dy 

It may not be an entry-level code, but instead of checking the bounds using the if statement, you can try to update the counter and handle the exception that occurs if the position is outside the grid:

 try: # Update counter. times[x][y] += 1 except IndexError: # Exit the simulation loop. break 

Hope this helps.

EDIT - comments on the second version:

Since you want to print the direction at each step, you can add this to the tuple:

 moves = [(0, 1, 'up'), (1, 0, 'right'), (0, -1, 'down'), (-1, 0, 'left')] 

Then you can replace the if statement, where you print the direction:

 dx, dy, direction = random.choice(moves) print('He moved', direction) 

When you use try-except, as in your current solution, you do not need to check the bounds in the while-statement. You can simply:

 while True: ... 

since the gap in the exception handler will exit the loop.

My final tip is to replace some number literatures with variables. Grid size, for example. appears in several places. You must create a variable and refer to it in the rest of the code:

 grid_size = 11 times = [0] * grid_size for i in range(grid_size): times[i] = [0] * grid_size 

Using a variable instead of numeric literals means that you just need to make changes in one place if you want to run code with a different grid size.

+8
source

I made a similar random walk program that allowed a drunk person to walk in any direction in three-dimensional space using spherical coordinates.

 import random import math def rw3(n,tries): s = 0 for m in range(1,tries+1): x = 0 y = 0 z = 0 pi = math.pi for step in range(1,n+1): t = random.uniform(0,2*pi) f = random.uniform(0,2*pi) p = 1 x += p*math.sin(f)*math.cos(t) y += p*math.sin(f)*math.sin(t) z += p*math.cos(f) s += (x**2+y**2+z**2)**.5 return s/tries life = 42 while life: n = int(input("Please enter the number of steps: ")) tries = int(input("How many times should I perform the experiment? ")) print() print(rw3(n,tries)) print() 
+1
source

Source: https://habr.com/ru/post/909221/


All Articles