Self-Avoiding Random Walks in 3D

SARW[maxLength_]:=(Clear[notvisitedQ];notvisitedQ[_]:=True;
First/@NestWhileList[{#,Select[Flatten[Outer[Plus,{#},{{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}},1],1],notvisitedQ]}&[notvisitedQ[#[[1]]]=False;RandomChoice[#[[2]]]]&,{{0,0,0},{{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}},#[[2]]=!={}&,1,maxLength-1])

Manipulate[SeedRandom[seed];With[{pts=SARW[max]},Graphics3D[{Thick,Gray,Line@pts,RGBColor[.6,.74,.36],Sphere[pts[[1]],1],RGBColor[.9,.42,.17],Sphere[pts[[-1]],1]},PlotLabel->Style[If[Length[pts]<max,"STUCK in "<>ToString[Length[pts]-1]<>If[Length[pts]==1," move!!"," moves!!"],"free after "<>ToString[max-1]<>If[Length[pts]-1==1," move"," moves"]],"Label",12],ImageSize->{500,500},PlotRange->All,Boxed->False]],{{max,50,"max steps"},1,10000,1},{{seed,1,"new walk"},1,1000000000,1},{{max,50},1,425,1,ControlType->None},AutorunSequencing->{{3,30}},SaveDefinitions->True]

Trace a path by moving at random from one lattice point to another while avoiding previously visited points.

Contributed by: Rob Morris

Based on a program by: Todd Rowland