Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

problems randomising survey using rand().

More
12 years 5 months ago - 12 years 5 months ago #82187 by zz07
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: 12 years 5 months ago by zz07.
The topic has been locked.
  • TMSWhite
  • TMSWhite's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 5 months ago #82233 by TMSWhite
Replied by TMSWhite on 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?
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 5 months ago #82234 by Mazi
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
The topic has been locked.
  • TMSWhite
  • TMSWhite's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 5 months ago #82235 by TMSWhite
Replied by TMSWhite on topic problems randomising survey using rand().
Mazi-

The blog post isn't quite accurate. The equation for "randnumber" should be:
Code:
{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
The topic has been locked.
More
12 years 5 months ago #82249 by zz07
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.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 5 months ago #82252 by Mazi
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
The topic has been locked.
More
6 years 10 months ago - 6 years 10 months ago #162307 by jkautto
Replied by jkautto on 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: 6 years 10 months ago by jkautto.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 10 months ago #162319 by tpartner
Replied by tpartner on 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
The topic has been locked.
More
6 years 10 months ago - 6 years 10 months ago #162323 by jkautto
Replied by jkautto on 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: 6 years 10 months ago by jkautto.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 10 months ago #162329 by tpartner
Replied by tpartner on 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.
The topic has been locked.
More
5 years 10 months ago #179364 by FeCanever
Replied by FeCanever on topic problems randomising survey using rand().
I have 8 study conditions, and have created 8 questions groups with a couple of questions each. I've been able to set up the question randomization inside each group (I need that) , but still haven't been able to set up the condition assignment so that each participant gets assigned a condition and is presented with one group of questions.
I have created an initial equation question and have inserted in the question field: if(random >= 1, random, (rand(1, 2)))

I then added ((random == 1)) and ((random == 2)) to the relevance field in each of the group settings. None of the group questions appears when I click to preview survey.

Should I add ((random == 1)) to each of the questions relevance box? Or is there a way of linking the assigned condition directly to the question group?

I'd really appreciate if anyone could help me out with that.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 10 months ago #179377 by tpartner
Replied by tpartner on topic problems randomising survey using rand().
It seems to me that the equation in question code "random" should be:

Code:
if(random >= 1, random, rand(1, 8))

Then in the relevance field for group-1:

Code:
random == 1

In the relevance field for group-2:

Code:
random == 2

And so on...

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose