Manipulate 2D random walk

The problem is that RandomInteger is reevaluated each time the manipulate is rendered (i.e. when the slider is dragged). You can fix that quite easily, by generating the random numbers outside of the manipulate and only using a prefix of the precomputed array:

steps = RandomInteger[{1, 4}, 10000];
Manipulate[
 rn = Accumulate[{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}[[Take[steps, n]]]];
 Graphics[{Line[rn], PointSize[Large], Green, Point[First@rn], Red, 
   Point[Last@rn]}], {n, 1, 10000, 1}]

Alternatively, you could re-seed the random number generator each time, so that RandomInteger always returns the same list, but that doesn't seem like a great idea to me:

Manipulate[
 SeedRandom[5];
 rn = Accumulate[{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}[[RandomInteger[{1, 4}, n]]]];
 Graphics[{Line[rn], PointSize[Large], Green, Point[First@rn], Red, 
   Point[Last@rn]}], {n, 1, 10000, 1}]

For better control over experiments made with this random walk model, I would write the code as follows.

With[{steps = 100},
  Manipulate[
    path = Accumulate[{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}[[Take[rndm, n]]]];
    Graphics[{
      Line[path],
      PointSize[Large], Green, Point[path[[1]]],
      Red, Point[path[[-1]]]}],
    {{path, {}}, None},
    {{rndm, rndmGen[]}, None},
    {n, 1, steps, 1, Appearance -> {"Labeled", "Open"}},
    Button["Reset", n = 1; rndm = rndmGen[], ImageSize -> All],
    ContentSize -> {400, 400},
    ControlPlacement -> Bottom,
    TrackedSymbols :> {n},
    Initialization :> (rndmGen[] := RandomInteger[{1, 4}, steps])]]

demo

  • Clicking on the Reset button re-initializes the model so a new experiment can be run.
  • Fixing the size of the content pane with ContentSize keeps the viewport steady as the experiment proceeds.