/
walk2Dv.py
36 lines (30 loc) · 1.24 KB
/
walk2Dv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def random_walk_2D(np, ns, plot_step):
xpositions = numpy.zeros(np)
ypositions = numpy.zeros(np)
moves = numpy.random.random_integers(1, 4, size=ns*np)
moves.shape = (ns, np)
# Estimate max and min positions
xymax = 3*numpy.sqrt(ns); xymin = -xymax
NORTH = 1; SOUTH = 2; WEST = 3; EAST = 4 # constants
for step in range(ns):
this_move = moves[step,:]
ypositions += numpy.where(this_move == NORTH, 1, 0)
ypositions -= numpy.where(this_move == SOUTH, 1, 0)
xpositions += numpy.where(this_move == EAST, 1, 0)
xpositions -= numpy.where(this_move == WEST, 1, 0)
# Just plot every plot_step steps
if (step+1) % plot_step == 0:
plot(xpositions, ypositions, 'ko',
axis=[xymin, xymax, xymin, xymax],
title='%d particles after %d steps' %
(np, step+1),
savefig='tmp_%03d.pdf' % (step+1))
return xpositions, ypositions
# Main program
from scitools.std import plot
import numpy, sys
numpy.random.seed(11)
np = int(sys.argv[1]) # number of particles
ns = int(sys.argv[2]) # number of steps
plot_step = int(sys.argv[3]) # plot each plot_step step
x, y = random_walk_2D(np, ns, plot_step)