# Display Dynamically the evolution of While

Assume that in the place of RandomInteger what obtains is the result of a simulation step (the $j-th$ step).

j = 0;
While[
j < 10,
RandomInteger[{0, 100},5];
j++
]


Also, assume that the maximum number of iterations (10 in the example above) is a parameter of the simulation. Consider that the same thing applies for the length of the output list (ie 5).

main[J_, x_] := Module[{j},
j = 0;
While[
j < J,
RandomInteger[{0, 100}, x];
j++
]
]


(I understand that main is not supposed to output anything, like this. In order to get an output, you can enclose RandomInteger in Sow and apply Reap on While. But that is not what I'm trying to do.)

Using a variation of the main function defined above in a Dynamic context produces nothing:

DynamicModule[{J, x, j},
Row@{
Column@{
Row@{"J", Null, Slider[Dynamic[J], {9, 13, 1}], Null, Dynamic[J]},
Row@{"x", Null, Slider[Dynamic[x], {2, 7, 1}], Null, Dynamic[x]},
},
Null,
Framed@main[Dynamic[J], Dynamic[x]]
}
]


This is the 'dynamic' main:

main[J_, x_] := DynamicModule[{j},
j = 0;
While[
j < Dynamic[J],
ListPlot[RandomInteger[{0, 100}, Dynamic[x]]];
j++
]
]


What am I doing wrong?

update

I have had success with a stripped down version of main:

main[Dynamic[J_], Dynamic[x_]] := DynamicModule[{},

ListPlot[RandomInteger[{0, 100}, x], Frame -> True]

]


(also, I had to insert another Dynamic between Framed and main: Framed@Dynamic@main[Dynamic[J], Dynamic[x]] in the calling code (see above))

The downside is that with this version, I can only visualize lists of varying Length (Dynamic[x]). I still have not figured out a way to visualize what goes on inside the loop (how to display a succession of Listplot's).

update2:

I wanted to replicate this (display tournaments dynamically, mostly). I have so far gotten to this (package) and this (Manipulate):

update3:

This is the package with the additional functionality for repeated tournaments (evolutionary-ish) and this is the Manipulate code:

any suggestions for improvements esp. on the Dynamic side of things is particularly welcome.

• Dynamic[J] is not a number, see: 5817 and linked topics. – Kuba Sep 2 '17 at 13:41
• As the QA linked by Kuba explains, Dynamic is purely for display. It has no room in any function which computes things (as opposed to display things computed elsewhere). – Szabolcs Sep 2 '17 at 15:14
• @Szabolcs: I've read the link (and the links in the link) @Kuba kindly provided. Let me see if I get this right: The only way to make an interactive display using the Dynamic functionality of some plots is to first generate them and then display them? – user42582 Sep 2 '17 at 15:21
• Yes, something like that. You can't compute with Dynamic[x]. Dynamic[x] is meant to be displayed, and it auto-updates whenever x changes. If you want to display a plot, use Dynamic@Plot[...]. – Szabolcs Sep 2 '17 at 15:23
• related: (71909) – WReach Sep 3 '17 at 13:44

You could just use Monitor:

main[j_, J_, x_] := (j = 0;
While[j < J, Pause[0.5]; RandomInteger[{0, 100}, x]; j++])
SetAttributes[main, HoldFirst]

Monitor[main[j, 10, 10], StringForm["Current value of j: ", j]]


I added a Pause so that you actually see the progress of this small example. The HoldFirst attribute is added so that the function works if the variable already has a value assigned (we need to pass a reference instead of a value)

• Monitor seems really powerful; the part in the documentation that states "... Any expression, including graphics and controls, can be given for mon " (my emphasis) seems promising. I'll have to look into it. – user42582 Sep 3 '17 at 15:25

If the only goal is to have some sort of Dynamic window into a long process (including tracking what plots are being generated and when) sometimes I'll make dynamic some argument to whatever holder function I've created to bundle data around.

i.e. say I've got some procedure that makes plots, cares about timestamp for some reason, and stores this stuff in some holder that gets passed around

ClearAll[f];
f[x_, holder_] :=
Module[{time = Last@(Date[] - holder["datestamp"])},
holder["currentPlot"] = Plot[y^x, {y, 0, 10}, Frame -> True, PlotLabel -> {y^x, time}];
holder["allPlots"] = Append[holder["allPlots"] /. holder[___] :> {},
{holder["plot"] , time}];
];


Then somewhere in a notebook for monitoring I'll execute something like:

Dynamic[holder["currentPlot"]]


Later if I execute:

x = -3; ClearAll[holder]; holder["datestamp"] = Date[];
While[ x <= 3,
f[x, holder]; holder["datestamp"] = Date[];
x += .1]


The Output cell of my earlier Dynamic[holder["plot"]] execution will now start displaying the various plots as they occur: