import numpy as np import random import matplotlib.pyplot as plt def adjacents(s,x,y): return (((x-1)%s,y%s),((x+1)%s,y%s),(x%s,(y-1)%s),(x%s,(y+1)%s)) def starttree(size): field = np.zeros((size,size)).astype('Bool') field[size/2][size/2] = True return field def browniantree(otree, steps): size = len(otree) for x in xrange(steps): npx, npy= random.randint(0, size), random.randint(0,size) while 1: adjs = adjacents(size, npx, npy) for apx, apy in adjs: if otree[apx][apy]: otree[npx][npy] = True print npx, npy break else: npx, npy = random.choice(adjs) continue break def display(otree): plt.imshow(otree, interpolation='nearest') plt.show() m = starttree(129) browniantree(m, 2000) display(m)