Re: Strange behavior: random and plot
- To: mathgroup at smc.vnet.net
- Subject: [mg102652] Re: [mg102726] Strange behavior: random and plot
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 23 Aug 2009 05:32:48 -0400 (EDT)
- Reply-to: hanlonr at cox.net
Since the definition of ran is only valid for a numerical argument, define it as such
Clear[ran];
ran[x_?NumericQ] := Random[Real, x];
Plot[ran[x] - ran[x], {x, 0, 3},
PlotStyle -> Directive[Thick, Red]]
Usually, what appears to be a problem with Plot is an effect due to its having attribute HoldAll
Attributes[Plot]
{HoldAll,Protected}
This is normally resolved by using Evaluate; however, it doesn't help here
Plot[Evaluate[ran[x] - ran[x]], {x, 0, 3},
PlotStyle -> Directive[Thick, Red]]
You need to use Random (or in v6 or later RandomReal) directly
Plot[Random[Real, x] - Random[Real, x],
{x, 0, 3}]
Plot[RandomReal[x] - RandomReal[x],
{x, 0, 3}]
Bob Hanlon
---- Don Krug <krugd at nku.edu> wrote:
=============
Hi group. I am new here and discovered some strange behavior (to me.)
Hope someone can help.
I will be teaching a course on programming with Mathematica this
semester and so you may hear from me some more.
I define
ran[x_]:=Random[Real,x]
If I evaluate ran[3]-ran[3] I get a non-zero result as expected.
But if I do
Plot[ ran[x]-ran[x] ,{x,0,3}]
I get an empty axes system. I assume it is the plot of the zero function.
I also know that if I set
ran2[x_]:=Random[Real,x]
then I get a non-zero plot as expected when I do
Plot[ ran2[x]-ran[x] ,{x,0,3}]
I am guessing the plot command is evaluating things in a strange way
that forces the two instances of ran to be the same.
Can anyone explain why?
Thanks
Don Krug
Northern Kentucky U