This produces a nice random walk plot:

randomWalk[n_] := Module[{steps, walk},   
   steps = 2 RandomInteger[{0, 1}, n] - 1;
   walk = Accumulate[steps];
   ListLinePlot[walk, ImageSize -> 200]]


random walk

But if I put it in a Do loop, nothing appears.

Do[randomWalk[200], 4]
  • 3
    $\begingroup$ As a general rule of thumb, try avoiding procedural loops in Mathematica. A better approach here could be Table[randomwalk[200], 4]. This will return its results in a list, whereas Do (or For, or While) do not return anything at all. $\endgroup$
    – MarcoB
    Commented Sep 10, 2022 at 2:31
  • $\begingroup$ Thank you. That gave me a result but no graphics - {randomwalk[200], randomwalk[200], randomwalk[200], randomwalk[200]} Is there a way to generate graphics as in the single case? You're right about Do, though. I've had a lot of trouble with it - something to avoid since it's not ready for prime time 😁 $\endgroup$ Commented Sep 11, 2022 at 3:08
  • $\begingroup$ Typo - it should have been Table[randomWalk[200], 4] (note the uppercase W in the function name). $\endgroup$
    – MarcoB
    Commented Sep 11, 2022 at 11:40

1 Answer 1


Because the Do does not return value. i.e what is generated inside the Do does not show up on the screen. You either need an explicit Print from inside the Do to see the output, or use Sow/Reap to collect the output. (or can also use a list and append to it, but Sow/Reap is supposed to be more efficient).

  Last@Reap@Do[Sow@randomWalk[200], 4]

Mathematica graphics

See this for reference

What has changed in Mathematica since version 2 so that Do no longer can be used to make animations?

There was a major overhaul of graphics in 2007 for V6, one aspect of which was that Mathematica switched from displaying graphics as a side-effect, to rendering directly in the notebook.

Since graphics are no longer a side effect, Do doesn't display the individual plots anymore.

  • 4
    $\begingroup$ Why using Reap+Do+Sow when a simple Table can be used? $\endgroup$ Commented Sep 10, 2022 at 11:10
  • 1
    $\begingroup$ @HenrikSchumacher Table can also be used ofcourse. I was just using the OP code, that is all. In Mathematica, there are at least 10 different ways to do the same thing, and one can not think of every one all the time. $\endgroup$
    – Nasser
    Commented Sep 10, 2022 at 17:20

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