Random Walker – Python Simulation

SyntaxHighlighter.config.bloggerMode=true; SyntaxHighlighter.config.clipboardSwf=’http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf’; SyntaxHighlighter.all(); I havent posted for long ..have been quite busy..here is some very simple code to animate random walk I just love seeing how the random walk simulates…experiment with it..and freely redistribute..;) Recently Im learing python simulations…so this was what i did while reading statistical mechanics by richel

# Random_walker.py
# Uses turtle class to simulate random walk through pseudo random generator
# external interrupt is enabled ,such that one can give external impacts
# to the random walker and see if that small external interrupts change random walk pattern
#1.give brisk shocks with up /right/left keys
#2.try combinations
# Aritra Kundu
# arsenous.blogspot.com


import turtle
import numpy as np
turtle.setup(400,500) # determine the window size
wn = turtle.Screen() # get a reference to the window
wn.title("Handling keypresses!") # Change the window title
tess = turtle.Turtle() # Create our favorite turtle

# The next four functions are our "event handlers".
def h1():
tess.forward(30)

def h2():
tess.left(45)

def h3():
tess.right(45)

def h4():
wn.bye() # Close down the turtle window

#These lines "wire up" keypresses to the handlers we've defined.
wn.onkey(h1, "Up")
wn.onkey(h2, "Left")
wn.onkey(h3, "Right")
wn.onkey(h4, "q")

wn.listen()

#listen responses from keyboard
N=1000 # no of steps
for i in xrange(1,N):
x=np.random.randint(-15,15) #step size for random walker,generate integer
theta=np.random.randint(0,360) # rotation angle
tess.forward(x)
tess.right(theta)

turtle.clear()
#wn.mainloop()