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
# It's much faster if we don't show the walk developing...

# Initialise screen, set size
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

def Exit():
# Blit everything to the screen
screen.blit(background, (0, 0))
# Save image
# Quit gracefully

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