Re: Relational Operators and Random Integers
- To: mathgroup at smc.vnet.net
- Subject: [mg90346] Re: [mg90305] Relational Operators and Random Integers
- From: DrMajorBob <drmajorbob at att.net>
- Date: Sun, 6 Jul 2008 07:21:29 -0400 (EDT)
- References: <7158674.1215250947191.JavaMail.root@m08>
- Reply-to: drmajorbob at longhorns.com
You TOLD Mathematica to recompute x randomly every time it's mentioned
(look up SetDelayed or ":=") and then you mentioned it four times.
Try instead
x = RandomChoice[{1, 2, 3}];
x
Which[x == 1, 1, x == 2, 2, x == 3, 3]
3
3
or
x := RandomChoice[{1, 2, 3}];
Switch[x, 1, 1, 2, 2, 3, 3]
2
In both examples, x is computed only once.
Bobby
On Sat, 05 Jul 2008 03:49:50 -0500, Peter Evans <peter.w.evans at gmail.com>
wrote:
> Hi all,
>
> I'm a new user of Mathematica 6 and am struggling with some basics. I
> wish to write a set of rules which are dependent upon a random variable.
> I've been using RandomChoice to choose my variable and then large If and
> Which statements to produce my desired dynamics.
>
> The problem is that the number that these statements end up spitting out
> aren't recognised as what they are in further If and Which statements.
> Here's a simple example that demonstrates my problem:
>
> In[1]:= x := RandomChoice[{1, 2, 3}]
> x
> Which[x == 1, 1, x == 2, 2, x == 3, 3]
>
> Out[2]= 1
>
> Out[3]= 2
>
> Mathematica clearly thinks x to be 1 but the If statement indicates its
> 2. What am I doing wrong here?
>
> Much thanks,
>
> Pete
>
>
--
DrMajorBob at longhorns.com