       Re: Relational Operators and Random Integers

• To: mathgroup at smc.vnet.net
• Subject: [mg90334] Re: Relational Operators and Random Integers
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Sun, 6 Jul 2008 07:19:14 -0400 (EDT)

```On 7/5/08 at 4:49 AM, peter.w.evans at gmail.com (Peter Evans) wrote:

>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:= x := RandomChoice[{1, 2, 3}] x Which[x == 1, 1, x == 2=
, 2, x
>== 3, 3]

>Out= 1

>Out= 2

>Mathematica clearly thinks x to be 1 but the If statement indicates
>its 2. What am I doing wrong here?

You are using SetDelayed to make your assignment to x.
SetDelayed causes RandomChoice to be evaluated everytime x is
referenced. Consider:

In:= x := RandomChoice[{1, 2, 3}]

In:= Which[x == 1, 1, x == 2, 2, x == 3, 3, True, 4]

Out= 4

What happened here is each  equality test fails since
RandomChoice did not select the specific value looked for by the
test. Consequently, the result was the default output of 4.

In order to get the behavior you seem to expect, you need to use
Set rather than SetDelayed.

That is:

In:= x = RandomChoice[{1, 2, 3}];
x

Out= 1

In:= Which[x == 1, 1, x == 2, 2, x == 3, 3]

Out= 1

```

• Prev by Date: Re: Converting a string to a variable name
• Next by Date: Re: Relational Operators and Random Integers
• Previous by thread: Re: Relational Operators and Random Integers
• Next by thread: Re: Relational Operators and Random Integers