Random Walk Programs

The One-Dimensional Random Walk

StepIncrements[n_] := Table[(-1)^Random[Integer], {n}]

StepIncrements[10]

FoldList[Plus, 0, StepIncrements[n]]

FoldList[Plus, 0, %]

Walk1D[n_] :=  FoldList[Plus, 0, Table[(-1)^Random[Integer], {n}]]

Walk1D[10]

The Two-Dimensional Lattice Walk

Walk2D[n_] := 
  FoldList[Plus, 
 {0, 0}, 
{{0,1},{1,0},{0,-1},{-1,0}}[[Table[Random[Integer, {1, 4}], {n}]]]]

Walk2D[10]

Mean Shape of a Two-Dimensional Lattice Walk

Apply[Plus, {xf, yf}^2]

SquareDistance[walk_List] := Apply[Plus, Last[walk]^2]

SquareDistance[ Walk2D[10] ]

MeanSquareDistance[n_Integer, m_Integer] := 
	Module[{walk2D},
	walk2D[s_] := FoldList[Plus, {0, 0}, 
{{0,1},{1,0},{0,-1},{-1,0}}[[Table[Random[Integer,{1,4}],{s}]]]]; 
	N[Sum[Apply[Plus, Last[walk2D[n]]^2], {m}]/m]
	]

MeanSquareDistance[10, 25]

Mean-Square, Radius of Gyration

cm = N[Apply[Plus, locs]/(n + 1)]

(Transpose[locs] - cm)^2

Apply[Plus,  Flatten[(Transpose[locs] - cm)^2]]

MeanSquareRadiusGyration[m_Integer, n_Integer] := 
  Module[{squareRadiusGyration},
      squareRadiusGyration[s_Integer] := 
         Module[{locs, cm, choices = {{1,0},{-1,0},{0,1},{0,-1}}},
          locs = FoldList[Plus,{0,0}, 
             choices[[Table[ Random[Integer,{1,4}],{s}]]]];
          cm = N[Apply[Plus, locs]/(s+1)];
          Apply[Plus, Flatten[(Transpose[locs] - cm)^2]]/(s+1)
                    ];
      N[Sum[squareRadiusGyration[n], {m}]/m]
              ]

MeanSquareRadiusGyration[10, 25]

The Critical Exponent of a Random Walk

data = Map[({MeanSquareDistance[#,75],#})&,
          Range[10, 90, 20]]

calculatedExponent = Fit[N[Log[data]], {x}, x]

calculatedGraph = Plot[calculatedExponent, {x, 0, 90}];

Needs["Graphics`Graphics`"]

dataGraph = LogLogListPlot[data,
		PlotStyle -> PointSize[.03] ];

Show[dataGraph, calculatedGraph] ;

Simple Graphics

ShowWalk2D[coords_, opts___]:=
	Show[Graphics[Line[coords],
		opts, AspectRatio -> Automatic]]

ShowWalk2D[ Walk2D[100] ] ;

ShowWalk2D[ Walk2D[10^4],
	DefaultColor -> Hue[.75],
	Background -> GrayLevel[0] ] ;