Different variables in protected fields

Discussions about CSEntry
Post Reply
MNBS
Posts: 9
Joined: January 28th, 2016, 2:21 pm

Different variables in protected fields

Post 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.
noel
Posts: 25
Joined: June 16th, 2012, 7:32 pm
Location: Libreville, Gabon

Re: Different variables in protected fields

Post 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.
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Different variables in protected fields

Post 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.
MNBS
Posts: 9
Joined: January 28th, 2016, 2:21 pm

Re: Different variables in protected fields

Post by MNBS »

Thanks a lot Gregory and Noel. I tried it and it worked perfectly :mrgreen: :D :mrgreen:
Post Reply