Self-Avoiding Random Walks

Manipulate[SeedRandom[seed];Module[{i=0,nmax=max,pts={{0,0}},moves={{-1,0},{0,1},{1,0},{0,-1}}},While[i<nmax&&Not@(And@@(MemberQ[pts,#]&/@Table[pts[[-1]]+moves[[i]],{i,1,4}])),i++;AppendTo[pts,RandomChoice[Select[Table[pts[[-1]]+moves[[i]],{i,1,4}],Not@MemberQ[pts,#]&]]]];Style[Column[{If[Length[pts]-1<nmax,"STUCK in "<>ToString[Length[pts]-1]<>" moves!!","free after "<>ToString[nmax]<>" moves"],Graphics[Line@pts,Epilog->{PointSize[Large],RGBColor[.6,.74,.36],Point[{0,0}],RGBColor[.9,.42,.17],Point[Last@pts],PointSize[Medium],Black,Table[Point[pts[[i]]],{i,2,Length[pts]-1}]},ImagePadding->30,ImageSize->{500,500},PlotRange->All]},Center],"Label",14]],{max,1,45,1,ControlType->None},{{max,1,"max steps"},1,1000,1},{{seed,10,"new walk"},1,1000000000,1},AutorunSequencing->{{1,25}}]

Trace a path by moving at random from one lattice point to another while
avoiding previously visited points. These so-called "self-avoiding random walks" are used in numerous physical models including polymer chains, protein folding and Brownian motion.

Contributed by: Rob Morris