Sei sulla pagina 1di 11

Random Walk- Python Program

2018-10-06
Internal II
PROGRAMMING, DATA
STRUCTURES, AND
ALGORITHMS IN PYTHON

Submitted by
Darshana Sudheer
IMSc Physics
I150112
Discuss and write python program to find distance
from initial point to final point in a non reversing
random walk. Iterate the program 10,000 times and
find average distance.

Random walk
Imagine a perfume bottle opened in the front of a classroom and the fragrance
soon drifting throughout the room. The fragrance spreads because some
molecules evaporate from the bottle and then collide randomly with other
molecules in the air, eventually reaching your nose even though you are
hidden in the last row. The model we shall develop to describe the path
traveled by a molecule is called a random walk. “Random” because it is chance
collisions that determine the direction in which a perfume molecule travels,
and “walk” because it takes a series of “steps” for the molecule to get from
here to there.

There are different types of random walk.We can classify on the basis of
memory

Random walk: A walk with no memory . The walk is recurrent – that is, the
walker returns to the starting point with probability. The expected number
of steps to return to the starting point is infinite.

Self-avoiding walk:A walk with unlimited memory .It is a sequence of moves


on a lattice that does not visit the same point more than once.

Non reversing walk: In between no memory and unlimited memory we can


have bounds on the amount of memory possessed by a walker. A walk with
memory 1 – that is, a walk in which the walker avoids the last spot visited – is
just a walk with no back-tracking, also known as a non-reversing walk.

Algorithm of Random walk


Consider N steps with 1 unit length.Since it is 2 dimensional it require two
variable(say x and y).According to question it can move west, east, north and
south. That is we have 4 cases which can be denoted as

West x=-1, y=0

East x =1, y=o

north x=0, y=1

south x=0 ,y=-1

Here x and y are initialized before starting the loop.

Generate these cases randomly by random function.0, 1, 2 &3 refers to north,


east, west and south respectively.Number of steps is 10.

As given below
x=2

y=2

for i in range(10):

arr=np.random.randint(0,4)

if (arr==0):

x=0

y=1

if(arr==1):

x=1

y=0

if(arr==2):

x=-1

y=0

if(arr==3):

x=0

y=-1

Code for Random walk


NON REVERSAL RANDOM WALK
In non reversal random walk, it doesn’t move to immediate old position. So if
first it moves north, then

x=0 y=1

Immediately it will not move to south, that is

x =0 y=-1

If particle moves west then it cannot move to east but it can move north, south
or more west.

According to above program,

A random number 2 is generated then next number cannot be 1 but it can be


0, 3 or 2. 0 cannot be paired with 3 and vice versa.

In code if random number 0 is generated, then it will look if previously


generated number is 3.If it is not 3 it can move, that is x and y value is
substituted and if it is 3, then x=0,y=0 is substituted and also it continues the
for loop. Continuing for loop means the entry 0 is ignored and moved forward.

s=4 s is previously generated random number.while initialising it requires

x=2 number other than 0,1,2,3

y=2
for i in range(10):

arr=np.random.randint(0,4)
if (arr==0):

if(s!=3):
x=0

y=1

else:
x=0

y=0
continue

if(arr==1):
if(s!=2):

x=1
y=0

else:
x=0

y=0

continue
if(arr==2):

if(s!=1):

x=-1

y=0
else:

x=0

y=0
continue

if(arr==3):
if(s!=0):

x=0

y=-1
else:

x=0
y=0

continue

if(s!=arr):

s=arr random number ‘arr’ is equated with s


Drawback of given non reversing walk program
When N=10 steps is given and generated numbers are 0,0,1,3,2,0,3,3,1,2
Then accordingy it moves
Random number Simple random walk n-reversing walk
0 north north
0 north north
1 east east
3 south south
2 west west
0 north north
3 south ignores
3 south ignores
1 east east
2 west ignores
10 steps Only 7 steps
So if the program is repeated it gives variable number of steps. It can be
improved

Distance
We assume that a “walker” takes sequential steps

The walker starts at the origin and take N steps in the x-y plane, each of length
1. The first step has a horizontal (x) component or a vertical (y) component.
The second step has a horizontal component or a vertical component ,
similarly in case of last step .

the distances along the x and y axes just add algebraically. Accordingly, the
total x distance from the origin is found out
Likewise, , the total y distance is found out.

Distance is square root of (x2 +y2)

Algorithm
 Add x value and y value separately for N steps(N=10).It should be done
inside the for loop.

Outside the for loop,find sum of (sum x)2 and (sum y)2.
h=0

k=0

x=2

y=2

Code for random walk

h=h+x

k=k+y

q=math.sqrt(h*h+k*k)

Code for distance


Application
Since a polymer chain is not a regular object and because it is subject to dynamic
structural equilibrium that involves motion and further, because polymers display
polydispersity in size, it is necessary to consider a statistical measure of a chain
size. It is considered as some random walk system. Here to find radius of gyration
and flori radius, it requires mean square distance value.
Reference

https://compmath.wordpress.com/walks-with-memory/

http://www.eng.uc.edu/~beaucag/Classes/IntrotoPolySci/End-to-
endDistance.htm

https://en.wikipedia.org/wiki/Self-avoiding_walk

A program is done on random walk (not on non reversing walk)


iterating about 10,000 times and finding average distance. It is
included here. It ‘s output is approximately 2.8.(other decimal points
are uncertain).

Potrebbero piacerti anche