I have code for a random walk

pt = Accumulate[{Sin@#, Cos@#} & /@ RandomReal[{0, 2 Pi}, 1000]];
boundary = {Min@pt, Max@pt};
ListLinePlot[pt, PlotRange -> {boundary, boundary}, AspectRatio -> 1]

which produces something like this:

Random Walk

I want to try to animate this, so that I can see how the random walk updates step by step with each new coordinate. I tried using DynamicUpdating but I couldn't really get it to work. Is there any way to achieve this? Any help is extremely appreciated!


1 Answer 1


Use ListAnimate. Also, you can make your path generation a bit simpler with AnglePath and your bounds are not correct as Min/Max will read from any coordinates, so I've used BoundingRegion instead:

pt = AnglePath[RandomReal[{0, 2 Pi}, 1000]];
bbox = Transpose[BoundingRegion[pt] /. Cuboid -> List];
 ListLinePlot[Take[pt, #], PlotRange -> bbox, AspectRatio -> 1] & /@ 
  Range[1, Length@pt]

random walk animation

Or alternatively, use a Manipulate:

 ListLinePlot[Take[pt, i], PlotRange -> bbox, AspectRatio -> 1],
 {i, 1, Length[pt], 1}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.