# Trying to do a 2D random walk for a particle

I am trying to perform a two-dimensional random walk for a particle, but I am not getting the desired result. The plot produced is a linear plot, which obviously should not have been. Similar code for a one-dimensional gives the desired result. Can someone please suggest where am I making any mistakes? Thanks.

import numpy
import random
TAIL = 2

a = numpy.zeros((2, 50))
b = a.transpose()
ns = 50                        # no of steps

for step in range(ns):
for _ in range(ns):
coin = random.randint(1,2)
b[step,0]+=1  # one unit length to the right
elif coin == TAIL:
b[step,1]-=1   # one unit length to the left

• What plot are you referring to? – nicoguaro Jun 9 at 16:35
• Your code says "If it's HEAD, go on to the right, else is its TAIL, go one down." As a consequence, you only ever move to the right or down, but never to the left or up. This is not the logic you wanted to implement, I suspect. – Wolfgang Bangerth Jun 9 at 16:44
• I still could not get you, sorry, what part of my code, do you want me to change? Thanks – Roshan Shrestha Jun 9 at 16:58

You are only moving in one direction for each axis.

coin_x = random.randint(1,2)

# Move on x axis
b[step,0]+=1
elif coin_x == TAIL:
b[step,0]-=1

# Move on y axis
coin_y = random.randint(1,2)
$$$$
`