7
$\begingroup$

Consider the first $10000$ digits of $\sqrt{2}$ and present them as a “random walk” by converting them in base 4 representing 4 directions. Following code produces this random walk

Manipulate[x = N[Sqrt[2], n];
walk = First@RealDigits[x, 4];
rn = FoldList[Plus, {0, 0}, {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}[[# + 1]] & /@ walk];
Graphics[{Line[rn], PointSize[Large], Green, Point[First@rn], Red, 
Point[Last@rn]}], {n, 1, 10000, 1}]

I want to do same manipulation for random integers (from $1$ to $ 4$).

I tried:

Manipulate[
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}]

But there is no fixed "Green" dot (initial point) in the graphic.

$\endgroup$
1

2 Answers 2

10
$\begingroup$

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}]
$\endgroup$
9
$\begingroup$

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.
$\endgroup$

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.