# Random Walks

I went to a nice talk today by Nicolas Le Novere about some current developments in SBML, a standard language for describing biological models to computers.

During his talk he mentioned some of his work on modelling neurons and flashed up an image representing a molecule diffusing into a synapse. I think it was undergoing Brownian motion, which is basically a random walk in space (looked like 2-dimensional space to me).

In May I wrote some Python code using PyGame which simulates random walks, generating still images of them, but also displaying an animation of their generation (see below). It generates anti-aliased lines, so it still looks smooth even when taking tiny step sizes. Simply run it, and it will show you how it is choosing the random steps in the walk, and finally it will produce a .png image which looks like this:

You can change the resolution, step-sizes etc. yourself. I am not sure if the steps should be normally distributed or uniformly distributed to be true Brownian motion, but I can't see any difference in the patterns generated by eye. Anyway, it's a nice simple example of using PyGame for something non-game related.

```import pygame, sys, numpy
from pygame.locals import *

# Define screen size
size=width,height=1000,1000
# It's much faster if we don't show the walk developing...
showwalk=True

# Initialise screen, set size
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('A Random Walk')

# Fill background with white
background = pygame.Surface(size)
background = background.convert()
background.fill((255, 255, 255))

# Start at centre of image
x,y=width/2,height/2

def Exit():
# Blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
# Save image
fname="RandWalk"+str(numpy.random.randint(0,99999))+".png"
pygame.image.save(background,fname)
# Quit gracefully
pygame.quit()
sys.exit(0)

# Start walking until we hit the edge
while 0<=x<width and 0<=y<height:
# Generate new steps
xnew=x+numpy.random.uniform(-1.0,1.0)
ynew=y+numpy.random.uniform(-1.0,1.0)
# Let's try to draw an anti-aliased line
pygame.draw.aaline(background, (0,0,0), (x,y), (xnew,ynew), True)
x,y=xnew,ynew
# Blit everything to the screen if we want to see development
if showwalk:
screen.blit(background, (0, 0))
pygame.display.flip()
# Exit gracefully if required by pressing q
for event in pygame.event.get():
if event.type == KEYDOWN and event.key==K_q:
Exit()
Exit()```