Here's a Python implementation of a self-avoiding random walk algorithm:
main.py1363 chars36 lines
This implementation generates an empty lattice of size n x n
and places a randomly chosen point in the middle of the lattice. It then performs m
steps of the walk by generating a list of valid directions to move in (i.e. directions that do not result in the walker revisiting a previously-visited point) and randomly choosing one from that list. If there are no valid directions to move in, the walk stops early. The function returns the resulting lattice, which contains a path of m
steps taken by the walker.
gistlibby LogSnag