problems randomising survey using rand().

More
6 years 4 months ago - 6 years 4 months ago #82187 by zz07
zz07 created the topic: problems randomising survey using rand().
Hi,

I have a survey (build Version 1.92+ Build 120608) with two sets of question groups. Each set contains 5 question groups. I want to randomise whether a participant is shown set 1 or set 2, but they are never shown both.

I have done this by creating both sets of question groups, and including an introduction group for the survey that is shown to everyone. In this introduction I have a question (equation type) which states rand(1,2), which I called 'random'.

For each question group in set 1 I have put (random==1) for relevance, and (random==2) for set 2.

This is the problem - the survey often displays the wrong set of questions. If 'random' is 2, it will display set 1. If 'random' is 1, it will also display set 1.

But if I change random to rand(2,2), it always (correctly) displays set 2 only. And if it is rand(1,1) it will always (also correctly) display set 1 only. So it seems that the relevance sections on the sets are not the problem.

I tried changing the relevance expression for set 1 to '(random == 1) and (random != 2)' or '(random != 2)', which should produce the same behaviour. But in fact it means that whenever random is 1, it displays set 2, and when random is 2, it still displays set 2.

I make sure to delete the cookies from my browser, just in case that is causing the problem - but it appears to have no effect.

I created a second survey as a test. I created 5 question groups, and gave each a different relevance equation - so question group 1 was (random==1) and question group 5 was (random==5). I created the same introduction question group and a 'rand(1,5)' equation question. Essentially the question group shown did not match the 'random' number generated. While only 1 group was ever shown, it was always a different group to the 'random' variable created.

Does anyone know why this is happening and of a solution to randomly showing 1 of 2 sets of questions?

I am currently using XAMPP while I design the survey.

Thank you very much for your help and feedback. I can upload the lss file if helpful.
Last Edit: 6 years 4 months ago by zz07.

Please Log in or Create an account to join the conversation.

More
6 years 4 months ago #82233 by TMSWhite
TMSWhite replied the topic: problems randomising survey using rand().
The most likely reason is that the rand() function is called each time the page is visited AND each time it is validated. Did you follow the recommendations of this example , which shows how to ensure that rand() is only called once during the survey?

Please Log in or Create an account to join the conversation.

More
6 years 4 months ago #82234 by Mazi
Mazi replied the topic: problems randomising survey using rand().
Your approach matches the suggested solution at this blog post at www.limesurvey-consulting.com : How to randomly show 1 of X questions at a Limesurvey survey

Only difference I can see is that the quotes are missing: ((randnumber=="1"))
...but this should make no difference.

You can test if this is fixed at the latest version by testing our online Demo which always runs the latest Limesurvey version.
Username: admin
Password: test
If you can reproduce the problem there please report back and attach your survey for testing.


Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in or Create an account to join the conversation.

More
6 years 4 months ago #82235 by TMSWhite
TMSWhite replied the topic: problems randomising survey using rand().
Mazi-

The blog post isn't quite accurate. The equation for "randnumber" should be:
{if(randnumber>0,randnumber,rand(1,2))}

Otherwise, the random value will change with each page, and even each time you try to validate the entered data.
The following user(s) said Thank You: kaled_fr

Please Log in or Create an account to join the conversation.

More
6 years 4 months ago #82249 by zz07
zz07 replied the topic: problems randomising survey using rand().
Hi,

Thank you both for the replies.

I looked at the example you (TMSWhite) suggested - I missed that one before.

I changed the equation expression to {if(random>=1, random, (rand(1,2)))} and now everything works fine. So you were right about it running the rand() multiple times.

Thanks very much. Very helpful.

Please Log in or Create an account to join the conversation.

More
6 years 4 months ago #82252 by Mazi
Mazi replied the topic: problems randomising survey using rand().
Thanks for the hint, Tom.

I think I had set it up the way explained at the blog and it worked fine. Is that related to the survey mode?
Because I think since the question is only called once (if you disable the back button) and thus the created random number is fixed?

How can the value change if I just use an ((randnumber=="2")) relevance at several questions?


Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in or Create an account to join the conversation.

More
9 months 1 week ago - 9 months 1 week ago #162307 by jkautto
jkautto replied the topic: problems randomising survey using rand().
Apparently this is still an issue with limesurvey 2.73, after 5 years or so? To pick a random question one MUST use the trick

{if (num>=1,num,rand(1,N))}

in order to make sure there is ONLY ONE random number generated per page.

I find that when using simple "{rand(1,N)}" the numbers in the response table are INCORRECT and there is no way to actually know which questions were shown to a participant (i.e., data is useless).

Can someone please verify that this trick is still the best way to go with 2.73?


ADDITION: My random questions have type "Text display", so no responses are stored in the result table. However, generated random numbers are stored and they are needed to find out visible texts.
Last Edit: 9 months 1 week ago by jkautto.

Please Log in or Create an account to join the conversation.

More
9 months 1 week ago #162319 by tpartner
tpartner replied the topic: problems randomising survey using rand().
Yes, this is the correct way to load a random number.

Sorry, I have no clue what you are asking in the "Addition".

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: jkautto

Please Log in or Create an account to join the conversation.

More
9 months 1 week ago - 9 months 1 week ago #162323 by jkautto
jkautto replied the topic: problems randomising survey using rand().
Thanks! That's really all I needed. I didn't have any other questions.

In my addition I just wanted to point out that "text display" is special as it does not store any data into response table. You REALLY need to store correct random values. With other question types even the simple {rand(1,N)} might suffice.
Last Edit: 9 months 1 week ago by jkautto.

Please Log in or Create an account to join the conversation.

More
9 months 1 week ago #162329 by tpartner
tpartner replied the topic: problems randomising survey using rand().
Yeah, this may be a good opportunity to use the custom question templates available in 3.x - based on a numeric-input. I'll play with that and report back.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!