4
$\begingroup$

The code sample:

Manipulate[
e = RandomVariate[NormalDistribution[0, sigma], n];
{a, e},
{{n, 3}, 1, 5, 1, Appearance -> "Labeled"},
{{sigma, 1}, 1, 2, Appearance -> "Labeled"},
{{a, 0}, 0, 10, 1, Appearance -> "Labeled"}
]

The problem: how to make a to change without updating e given n

and sigma unchanged.

Difference with suggested solutions: random vars depend on other controls (i.e. n and sigma) of Manipulate body.

$\endgroup$
  • $\begingroup$ A quick and dirty hack would be fixing the seed (BlockRandom). $\endgroup$ – Szabolcs Jul 29 '15 at 6:24
  • $\begingroup$ @Szabolcs, is there some more flexible solution? $\endgroup$ – garej Jul 29 '15 at 6:35
6
$\begingroup$

Body of Manipulate is wrapped by Dynamic and Dynamic doesn't know what's inside inner Dynamics, that's how we can screen a variable to not prompt the very outer Dynamic to evaluate:

Manipulate[
 e = RandomVariate[NormalDistribution[0, sigma], n];
 {Dynamic@a, e}, 
 {{n, 3}, 1, 5, 1,   Appearance -> "Labeled"}, 
 {{sigma, 1}, 1, 2,  Appearance -> "Labeled"}, 
 {{a, 0}, 0, 10, 1,  Appearance -> "Labeled"}
]
$\endgroup$
  • $\begingroup$ Thank you for the answer. Just as a follow up, what if I want to use a and e in the same expression, say, a + e. How Dynamic wrapper can be used in this case? $\endgroup$ – garej Jul 29 '15 at 7:56
  • 1
    $\begingroup$ @garej for full controll over evaluation I'd go with DynamicModule and manually written solution. But in this qase when you put Dynamic[a + e] it still works right, doesn't it? $\endgroup$ – Kuba Jul 29 '15 at 8:03
  • $\begingroup$ right! Now I clearily see the point. Thank's for a tip. $\endgroup$ – garej Jul 29 '15 at 8:08
  • $\begingroup$ @garej great :) good luck. $\endgroup$ – Kuba Jul 29 '15 at 8:12
  • $\begingroup$ @Kuba A quick question. Why doesn't wrapping e in Refresh work here? Refresh[e = RandomVariate[NormalDistribution[0, sigma], n], TrackedSymbols :> {sigma, n}]; I thought for sure this was an alternative but it does not work. $\endgroup$ – Edmund Jul 29 '15 at 10:10

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.