2
$\begingroup$

I have code for a random walk

pt = Accumulate[{Sin@#, Cos@#} & /@ RandomReal[{0, 2 Pi}, 1000]];
boundary = {Min@pt, Max@pt};
Norm@Last@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!

$\endgroup$
6
$\begingroup$

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];
ListAnimate[
 ListLinePlot[Take[pt, #], PlotRange -> bbox, AspectRatio -> 1] & /@ 
  Range[1, Length@pt]
 ]

random walk animation

Or alternatively, use a Manipulate:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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