Page 1 of 1

Different variables in protected fields

Posted: February 4th, 2016, 3:48 am
by MNBS
Hi CSPro users,
I want to create 2 random variables X and Y and they should be different (X <> Y). I tried the next code :

numeric X;
numeric Y;
X = random (1,10);
Do Y = random (1,10) until Y <> X;
enddo;


I also tried that
numeric X;
numeric Y;
X = random (1,10);
Y= 0;
While X <> Y do Y = random(1,10);
enddo


However, it did not work. Could you please help me and correct the code.
Note : X and Y are protected fields. Therefore, I did not use the "reenter" function.
Thanks everyone.

Re: Different variables in protected fields

Posted: February 4th, 2016, 10:05 am
by noel
Please, try this other logic.

seed(systime());
numeric x = random(1,10);

numeric y = x,t = 7;

while y = x do
seed(t*systime());
inc(t,3*t);
y = random(1,10);
enddo;

I tried it even with the protected fields.

Re: Different variables in protected fields

Posted: February 4th, 2016, 1:38 pm
by Gregory Martin
The problem with that initial code is that CSPro uses = as both an assignment operator and a conditional operator (does X equal Y?). In your loop it was being used as a conditional operator when you instead wanted it used as an assignment operator. You could modify your code to this:
seed(systime()); // optional

numeric X = random(1,10);
numeric Y = X;

while X = Y do
    Y =
random(1,10);
enddo;
That is similar to what noel posted above, but you don't need to keep on seeding the random number generator in the loop.

Re: Different variables in protected fields

Posted: February 4th, 2016, 3:41 pm
by MNBS
Thanks a lot Gregory and Noel. I tried it and it worked perfectly :mrgreen: :D :mrgreen: