Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

A Dynamic mess

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92925] A Dynamic mess
  • From: chandler.seth at gmail.com
  • Date: Mon, 20 Oct 2008 07:32:31 -0400 (EDT)

There has just got to be an easier way to do the following. The
example is somewhat silly, but if I could understand how to do this
competently, I would learn a lot.

Suppose I want to compute the sum of an integer  (z) and a value
chosen from some uniform distribution on the interval 0 to a. I want
to do this interactively and I do NOT want the draw from the random
distribution to change when I change z, just when I change the
parameter a. So, we create the number z as follows:

z=6;SetterBar[Dynamic[z],{6,7,8}]

If I do the following, it does not work as expected because, I
suppose, Dynamic[z] is a FrontEnd object in its heart of hearts (a
DynamicBox) and thus does not add to 2 the way an integer would.

Dynamic[z]+2

So, this can be fixed with Dynamic[z+2]

OK, now instead of adding 2, let's add some random value

a=0.5;Slider[Dynamic[a]];

Dynamic[z+RandomReal[{0,Setting@Dynamic[a]}]] (* setting is added
because if one just uses Dynamic[a] one is referencing a front end
object rather than an actual number*)

This evaluates, but it does not have the desired behavior. Now, when I
change z I also get a new draw from the random distribution. I thought
of trying to blocking the redraw by wrapping Dynamic around the
RandomReal, like this.

Dynamic[z+Dynamic@RandomReal[{0,Setting@Dynamic[a]}]]

But that doesn't work at all. It still redraws when I change z and the
addition no longer works in the desired way since the Dynamic
expression is not "really" a number but just a box.

I can solve the latter problem by wrapping a Setting in front of the
Dynamic

Dynamic[z + Setting@Dynamic[RandomReal[{0, Setting@Dynamic[a]}]]]

Now it adds properly but it still refreshes the random draw every time
I change z.

This doesn't work either and the expression is getting awfully
complicated

Dynamic[z +
  Refresh[Setting@Dynamic[RandomReal[{0, Setting@Dynamic[a]}]],
   TrackedSymbols -> {a}]]

And this doesn't work either

Dynamic[Setting@Dynamic[z] +
  Setting@Dynamic[RandomReal[{0, Setting@Dynamic[a]}]]]

Can someone help!


  • Prev by Date: Re: notation using # with exponents and &
  • Next by Date: Re: Pi Formula
  • Previous by thread: Re: ReplaceAll on Nested Expressions
  • Next by thread: Re: A Dynamic mess