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.
Different variables in protected fields
Re: Different variables in protected fields
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.
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.
-
- Posts: 1796
- Joined: December 5th, 2011, 11:27 pm
- Location: Washington, DC
Re: Different variables in protected fields
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.numeric X = random(1,10);
numeric Y = X;
while X = Y do
Y = random(1,10);
enddo;
Re: Different variables in protected fields
Thanks a lot Gregory and Noel. I tried it and it worked perfectly