Welcome to the LimeSurvey Community Forum

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

Struggling with rendomizing

  • fi1Fatima
  • fi1Fatima's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 months 5 days ago #266442 by fi1Fatima
Struggling with rendomizing was created by fi1Fatima
Hello!
I'm conducting a survey where there are 9 different conditions, each with 6 sets of questions. I want to randomize these 9 conditions so that each respondent sees only 3 of them. For example, one respondent might see conditions 1, 5, and 9, while another might see conditions 3, 5, and 7, and so on.I’ve set up a hidden question using the
Code:
rand(1,9)
function, and I've assigned group numbers from 1 to 9 to each condition, this way each respondent could see one condition.

I also setup a hidden question with following equation: shuffleArray([1,2,3,4,5,6,7,8,9])[0] + ',' + shuffleArray([1,2,3,4,5,6,7,8,9])[1] + ',' + shuffleArray([1,2,3,4,5,6,7,8,9])[2]. Then in each randomizatioon group I use "Showcond1 == 1". Still, one condition is being visible each time I preview the survey.  However, I'm not sure how to configure LimeSurvey so that each participant can see 3 random conditions instead of just one.Could someone please provide clear instructions on how to achieve this?

I will appriciate your help.

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 5 days ago #266446 by holch
Replied by holch on topic Struggling with rendomizing
No reason to post twice. Everyone who posts for the first time needs to go through a review process, as we had enormous issues with spam posts.

I have chosen to approve this one, as it contains more information than the other post you made (it has been deleted).

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 5 days ago - 4 months 5 days ago #266454 by Joffm
Replied by Joffm on topic Struggling with rendomizing
Hi,
there are some misunderstandings.
1. rand(1,9)
As you saw this function generates ONE random number between 1 and 9.
Now you could think "Oh, I just generate three random numbers 1-9". But here you might get 4, 7, 4 - doubles.
So this approach is not usable.

2. shuffleArray([1,2,3,4,5,6,7,8,9])[1]
Where did you find this function? It is not an implementd function of LimeSurvey.
These you find here
[url] www.limesurvey.org/manual/ExpressionScri...mplemented_functions [/url]

Well, now the solution(s)
1. If you have rights to use javascript (unfortunately you did not answer the questions at the beginning) you create a question of type "short text" (eq1).
In the source code of this question you enter this script.
BTW: Here you find this function "shuffle"
Code:
<script type="text/javascript" charset="utf-8">
 
  function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;
    // While there remain elements to shuffle...
    while (0 !== currentIndex) {
      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;
      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }
    return array;
  }
 
$(document).on('ready pjax:scriptcomplete',function(){
  // Fill the array
  var arr = [1,2,3,4,5,6,7,8,9];
  arr = shuffle(arr).slice(0,3).join('');
  $('#question{QID} input[type="text"]').val('#' + arr);
 
  $('#question{QID}').hide();
});
</script>


You will get something like
 

And now you have the condtions of your groups.
group1 is displayed if this string contains "1"
group2 is displayed if this string contains "2"
group3 is displayed if this string contains "3"
​​​​​​​...

To check you use the function strpos(haystack,needle)
So.
group1: strpos(eq1,"1")>0
group2: strpos(eq1,"2")>0
​​​​​​​group3: strpos(eq1,"3")>0
...

Why did I add the heading "#".
Without this the result of strpos("241","2") would be 0 (strpos starts counting at 0).
But a result of 0 also means "not found"
To avoid this there is the "#".

2. If you have no rights to use javascript you can do this with only ExpressionScript.
But this is a bit longer (you need 10 single equations), but really very straightforward.

If you need this solution, send the lss export of your survey , and I'll show.

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 4 months 5 days ago by Joffm.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose