ValueSet.Randomize Function

Discussions about CSEntry
Post Reply
Vicmarfe
Posts: 12
Joined: September 12th, 2018, 2:53 pm

ValueSet.Randomize Function

Post by Vicmarfe »

Hi team:
I use the ValueSet.Randomize function in a question's logic, to randomize its ValueSet, as follows:
VariableName.VS1.Randomize(Exclude(8,9));
However, every time the question is displayed, the values ​​are always displayed from 1 to 9, they are only becomes randomly when moving back from the next field.
How can I get them to be displayed randomly from the first time the question appears on the device screen, and in all cases?
Thanks in advance.

Vicmar
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: ValueSet.Randomize Function

Post by Gregory Martin »

Random numbers in the computer world are rarely truly random, as an algorithm determines in what order numbers appear. What you need to do to get randomness in the sequence is to call the seed function: https://www.csprousers.org/help/CSPro/s ... ction.html

A common procedure is to "seed" the random number generator with the time, like this:
seed(timestamp());
If you do this at the beginning of your program, you will get different random numbers throughout.
Vicmarfe
Posts: 12
Joined: September 12th, 2018, 2:53 pm

Re: ValueSet.Randomize Function

Post by Vicmarfe »

Thanks Gregory
Now randomize works as we need.
However, we have an additional problem, we use the following code to remove the option selected in the previous question (P2);

PROC P2A
Preproc
ValueSet r=P2A_VS1;
r.remove(P2);
setvalueset(P2A,r);
seed(timestamp());
P2a_VS1.Randomize(Exclude(8,9));


Options are always displayed from 1 to 9, never random. Is it possible to randomize this question?

Best.
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: ValueSet.Randomize Function

Post by Gregory Martin »

You're setting a custom value set for P2A, so it won't be using P2A_VS1. You should randomize r:
PROC P2A
Preproc
ValueSet
r=P2A_VS1;
r.remove(P2);
seed(timestamp());
r.Randomize(Exclude(8,9));
setvalueset(P2A,r);
Vicmarfe
Posts: 12
Joined: September 12th, 2018, 2:53 pm

Re: ValueSet.Randomize Function

Post by Vicmarfe »

Thanks Gregory
Now everything is working fine
Your help has been very useful.

Happy weekend.
Post Reply