Friday, September 18, 2009

random walker 2

With the first random walker done and tracing its path, the next thing to do is to make a simulation of a many random walkers that appear as particles moving around the canvas.

Here is the code of a single random walker and a snap shot.

'''
animation of a single random walker
'''

from pylab import *
from numpy import *
import time

ion()

tstart = time.time()

#cheating canvas size

wx = linspace(1,100,100)
wy = linspace(1,100,100)
plot(wx,wy,'white')

steps = 500
xinit = 50
yinit = 50

x = [xinit]
y = [yinit]
rw, = plot(x,y,'go')

for i in arange(1,steps):
d = random.randint(1,5) #selects num from 1,2,3,4
if d == 1:
x = [x[0] + 1]
rw.set_xdata(x)
elif d == 2:
x = [x[0] - 1]
rw.set_xdata(x)
elif d ==3:
y = [y[0] + 1]
rw.set_ydata(y)
elif d == 4:
y = [y[0] - 1]
rw.set_ydata(y)
draw()



















Next code, N random walkers.

*** tomorrow is software freedom day, UST Medicine Auditorium

No comments:

Post a Comment