Random Walk—2-Dimensional

Author

Eric W. Weisstein
March 3, 2009

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Statistics/RandomWalk2-Dimensional.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/RandomWalk2-Dimensional.html.

©2009 Wolfram Research, Inc. except for portions noted otherwise

2-D Unit Steps

V5

TwoPi=N[2Pi];
RandomWalk2D[N_]:=Module[{l={{0,0}},x={0,0},i=0,th},
  Do[
    th=TwoPi Random[];
    x+={Cos[th],Sin[th]};
    l=Append[l,x],
    {N}
  ];
  l
]

rw=RandomWalk2D[500];
Show[Graphics[{Line[rw],{PointSize[.05],Point[rw[[-1]]]}},Axes->True],AspectRatio->Automatic]

V6

RandomWalk2D[n_]:=Accumulate[Through[{Cos,Sin}[#]]&/@RandomReal[{0,2π},n]]

RandomSeed[1];
rw=RandomWalk2D[500];
Show[Graphics[{Line[rw],{PointSize[.02],Point[rw[[{1,-1}]]]}},Axes->True],AspectRatio->Automatic]

RandomSeed[1];
rw=RandomWalk2D[2000];
Show[Graphics[{Line[rw],{PointSize[.02],Point[rw[[{1,-1}]]]}},Axes->True],AspectRatio->Automatic]

rw=Norm/@RandomWalk2D[10^6];

Histogram[BinCounts[rw],50,PlotRange->All]

RandomWalk2D[n_]:=Norm[Total[Through[{Cos,Sin}[#]]&/@RandomReal[{0,2π},n]]]

With[{n=10^6},{RandomWalk2D[n],(Sqrt[n])//N}]//Timing

With[{n=10^6},{RandomWalk2D[n],(Sqrt[n])//N}]//Timing

With[{n=10^7},{RandomWalk2D[n],(Sqrt[n])//N}]//Timing

With[{n=10^7},{RandomWalk2D[n],(Sqrt[n])//N}]//Timing

2-D Random Step Lengths

RandomWalk2D[n_,Steps->Random]:=Accumulate[Through[{Re,Im}[#]]&/@RandomComplexDisk[1,n]]

RandomSeed[1];
rw=RandomWalk2D[500];
Show[Graphics[{Line[rw],{PointSize[.02],Point[rw[[{1,-1}]]]}},Axes->True],AspectRatio->Automatic]

r2r[n_]:=Sqrt[#.#]&[Plus@@Table[{Cos[#],Sin[#]}&[Random[Real,{0,2Pi}]],{n}]]

hr=BinCounts[Table[r2r[1000],{20000}],{0,50,1}]

graph2=BarChart[hr,BarGroupSpacing->0,PlotRange->All,Ticks->None]

2-D Lattice Unit Steps

Lattice

RandomWalk2DLattice[n_]:=Accumulate[Through[{Cos,Sin}[# π/2.]]&/@RandomInteger[3,{n}]]

rw=RandomWalk2DLattice[500];
Show[Graphics[{Line[rw],{PointSize[.02],Point[rw[[{1,-1}]]]}},Axes->True],AspectRatio->Automatic]

r2u[n_]:=Sqrt[#.#]&[Plus@@Table[{Cos[#1],Sin[#1]}#2&[Random[Real,{0,2Pi}],Random[]],{n}]]

hu=BinCounts[Table[r2u[1000],{20000}],{0,50,1}]

graph1=BarChart[hu,BarGroupSpacing->0,PlotRange->All,Ticks->None]