/ Published in: Python
Expand |
Embed | Plain Text
import numpy import random import copy from pyx import * class RandomWalker: def __init__(self, dim = 2, max_step = 100): self.dim = dim self.origin = numpy.zeros(dim) self.max_step = max_step self.trajectory = [self.origin] self.position = copy.copy(self.origin) self.time = 0 pass def walk(self, step=1): for k in range(step): self.time += 1 direction = random.randint(0,self.dim-1) self.position[direction] += [-1,1][random.randint(0,1)] self.trajectory.append(copy.copy(self.position)) def clear(self): self.__init__(self.dim) def plot(self, filename): c = canvas.canvas() x_min = min([i[0] for i in self.trajectory]) x_max = max([i[0] for i in self.trajectory]) y_min = min([i[1] for i in self.trajectory]) y_max = max([i[1] for i in self.trajectory]) p = path.path(path.moveto(self.origin[0]-x_min,self.origin[1]-y_min)) for i in range(self.time-1): x,y = self.trajectory[i+1] p.append(path.lineto(x-x_min,y-y_min)) c.stroke(p) c.writeEPSfile(filename) c.writePDFfile(filename)
You need to login to post a comment.
