The following function generates a two-dimensional random walk in which a particle starts at the origin and takes successive random steps of  length one. The randomness is obtained from the Mathematica function Random, which uses a sophisticated number-theoretical algorithm to generate a random-like sequence of numbers in the interval [0,1]. The algorithm is itself deterministic, which arises an interesting paradox: how can a deterministic method generate random-like features?

To run the function, first activate the cell: click the right vertical bracket to select and then hit "Shift-Enter". Then, in a new cell, call the function with some integer argument, hte number of steps that you want the particle to take. For example, RandomWalkd2D[900] will generate a random walk of 900 successive steps and plot the resulting path of the particle. The function also plots a circle of radius Sqrt[N], to indicate where, on average, the  particle should end up after N steps.

For this course, you do not have to understand all the details of this Mathematica function, e.g., you don't have to know the Module, Do, and Graphics commands although it would be worth your effort to do so. But I do expect you to be able to run the function and understand the significance of the output.

RandomWalk2D[ nsteps_ ]:=Module[
{
path={ {0,0} },
vec ={0,0},
i=0,
rw,
sqrtsteps = Sqrt[ N[nsteps ] ],
th,
twoPi = 2. Pi
} ,

Do[
       th=twoPi Random[];
        vec+={ Cos[th],Sin[th] };
        path=Append[ path ,vec ],
       { nsteps }
     ];
 
    g1 =Graphics[
{ 
Line[ path ], 
{ PointSize[.03],Point[ path[[-1]] ] } 
},
Axes->True
] ;

g2 = Graphics[ Circle[ {0., 0.}, sqrtsteps ]  ];

Show[ g1, g2 , 
PlotRange -> {
{-1.5 sqrtsteps, 1.5 sqrtsteps},
{-1.5 sqrtsteps, 1.5 sqrtsteps} 
} 
]

]

RandomWalk2D[ 900]   (* activate this cell repeatedly to see different realizations *)

The function ensembelAverage below calculates the ensemble average of the cosine of the angle between two unit vectors (Subscript[x, i]) and (Subscript[x, j ]), cos((Subscript[θ, ij])), when the "experiment" is repeated "nexpt" times. You can see how the ensemble average<(Subscript[x, i])·(Subscript[x, j])≥<cos((Subscript[θ, ij]))> is never exactly zero because, for a finite number of random choices of angles, the probability of an exact cancellation is essentially zero. But as the number of experiments increase, say from 10 to 100 to 1000 to 10,000, the ensemble average of the dot product steadily gets smaller. Can you figure out the mathematical rule that governs how rapidly the ensemble average of the dot product converges to zero as the number N of experiments increases?

ensembleAverage[ nexpts_ ] := (1./n) Total[ Table[ Cos[ 2. Pi Random[] ] , {nexpts} ] ]

ensembleAverage[100000]

ListPlot[ Table[ ensembleAverage[10^i], {i,1,6}] ]

The function RandomWalkd2DFinalPoints executes “nwalks” random walks each of length “nsteps” and then plots the positions of the final points of the random walks. The resulting cloud of points gives you a direct sense of the probability for the random walk to end up a certain distance from the origin.

RandomWalk2DFinalPoints[ nwalks_, nsteps_ ]:=Module[
{
cloud = {} ,   (* set of endpoints of random walks. *)
vec ,   
sqrtsteps = Sqrt[ N[nsteps ] ],
th,
twoPi = 2. Pi 
} , 

Do[          (* loop over number of walks nwalks *)

vec = {0., 0. } ;   (* initialize walk to origin *)

Do[    (* loop over number of steps, nsteps *)
th=twoPi Random[];
              vec+={ Cos[th],Sin[th] } ,
             { nsteps }
            ] ;

cloud = Append[ cloud, vec ] ,  (* collect end point of random walk *)

{ nwalks } 

] ;
 
    g1  = Graphics[  { PointSize[Small], Blue, Point[ cloud ] } ] ;

g2 = Graphics[ Circle[ {0., 0.}, sqrtsteps ]  ];

Show[ g1, g2 , 
PlotRange -> {
{-2 sqrtsteps, 2 sqrtsteps},
{-3sqrtsteps, 2 sqrtsteps} 
} 
]

]

RandomWalk2DFinalPoints[ 5000, 400]