0

# Random Walk in Python Code

Tue 23 Feb, 2016 07:53 am
I need to modify this code to show a random walk on a 10*10 matrix. The walker will move by one element at a time labeled A through Z by moving up,down,left or right. The walker can never go outside of the matrix either.

#You need to debug and modify the program so that it is able to do random walking
import random

N = 10
FILLER = '.'

#create a list to represent a row
#The row at first is filled with FILLER '.'
line = []
for i in range(N):
line.append(FILLER)

#create a matrix
#The matrix is the maze to walk
a=[]
for i in range(N):
line = line[:]
a.append(line)

#always starts at the left top corner of the matrix
#fill the matrix cell at the left top corner 'A'
x = 0
y = 0
letter = 'A'
a[x][y] = letter

#a sequence of random walk steps
#The below loop will settle the next letter
letter = chr(ord(letter) + 1)
#Where to go from the current sell: right, left, down, or up?
#We need a bit of randomness
direction = random.randrange(0, 4);
#keep track of the directions tried
moves_tried = 0 #trials to put a letter

#The following loop broken either due to
#running out of letters or
#having tried four directions
while (moves_tried < 4 and letter <= 'Z'):
#turn is random but its value is always between 0 and 3
turn = (direction + moves_tried) % 4
if (turn == 0): #right
new_x = x
new_y = y + 1
elif (turn == 1): #left
new_x = x
new_y = y - 1
elif (turn == 2): #down
new_x = x + 1
new_y = y
else: #up
new_x = x - 1
new_y = y
#check the new position (new_x, new_y) to see if the letter can be settled
if (0 <= new_x and new_x < N and 0 <= new_y and new_y < N and a[new_x][new_y] == FILLER):
#label the target element and prepare the next iteration

else:
moves_tried = moves_tried + 1
for i in range(N):
#use (' '.join(a)) to get a string
#print the ith row a of the matrix
• Topic Stats
• Top Replies
Type: Question • Score: 0 • Views: 2,206 • Replies: 1
No top replies

steve reid

1
Fri 30 Jun, 2023 01:48 pm
@Cbr600,
Code:``` from random import choice data = [[' ' for i in range(10)] for j in range(10)] char, x, y, counter, data[x][y] = 66, 0, 0, 0, 'A' while char < 91: xc, yc = x, y direction = choice((-1, 1)) if choice((False, True)): x += direction if x == -1: x = 0 if x == 10: x = 9 else: y += direction if y == -1: y = 0 if y == 10: y = 9 if data[x][y] == ' ': data[x][y] = chr(char) char += 1 else: x, y = xc, yc counter += 1 if counter > 99999: print('Boxed in\n') break for item in data: print(item) input('\nPress the <ENTER> key to continue') ```

I realise this post is 7 years old, but with a liking for puzzles and an interest in python coding I had a crack at it

The python code posted above seems to do what is required when entered into my Python Launcher for Windows (Console)

There are two possible outputs

1. Something like this, when A to Z completes

Code:``` ['A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] ['B', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', 'D', 'E', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', 'F', 'G', ' ', ' ', ' ', ' ', ' ', ' '] [' ', 'J', 'I', 'H', ' ', ' ', ' ', ' ', ' ', ' '] [' ', 'K', 'N', 'O', 'P', ' ', 'Z', ' ', ' ', ' '] [' ', 'L', 'M', 'R', 'Q', 'X', 'Y', ' ', ' ', ' '] [' ', ' ', ' ', 'S', 'T', 'W', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', 'U', 'V', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] ```

2. Something like this, when a point is reached where there is nowhere to move

Code:``` Boxed in ['A', 'B', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' '] ['J', 'K', 'D', 'E', ' ', ' ', ' ', ' ', ' ', ' '] ['I', 'H', 'G', 'F', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] ```
0 Replies

### Related Topics

HVAC lines - Question by alevythiebaut
css - Question by keerthi60
Hacked on Facebook - Question by Hopea
need help with this code - Question by Philoue
Hide/Unhide Columns - Question by N Prakash

1. Forums
2. » Random Walk in Python Code