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$
2
  • $\begingroup$ A quick and dirty hack would be fixing the seed (BlockRandom). $\endgroup$ Commented Jul 29, 2015 at 6:24
  • $\begingroup$ @Szabolcs, is there some more flexible solution? $\endgroup$ Commented Jul 29, 2015 at 6:35

1 Answer 1

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$
12
  • $\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$ Commented Jul 29, 2015 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$ Commented Jul 29, 2015 at 8:03
  • $\begingroup$ right! Now I clearily see the point. Thank's for a tip. $\endgroup$ Commented Jul 29, 2015 at 8:08
  • $\begingroup$ @garej great :) good luck. $\endgroup$ Commented Jul 29, 2015 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$ Commented Jul 29, 2015 at 10:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.