Welcome to the LimeSurvey Community Forum

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

Selecting pairs of questions from two pools of question groups in a random order

  • Moathh
  • Moathh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 3 months ago - 4 years 3 months ago #213674 by Moathh
Hello All,I have two groups of questions, Group A (A1, A2,…, A90) and Group B (B1, B2, …,B90), where each question in Group A is paired with a question in group B (i.e. Ax is related to Bx). I want to randomly display 10 pairs of questions (i.e. 10 from A and their corresponding pair that has the same number from B ), however, I do not want to show the pairs in a consecutive manner but rather randomly. For example, I want to show (A1, B3, A5, B1, B5, A3 ..) and not (A1, B1, A5, B5, B3, A3 …). I will be thankful if you can guide me on how to go on about it.
Many thanks!
Moath
Last edit: 4 years 3 months ago by Moathh. Reason: typo
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 3 months ago #213675 by Joffm
Hi,
more or less the same procedure as shown here
[url] forums.limesurvey.org/forum/can-i-do-thi...-each-question-group [/url]

Create a question of type "short text" ("Q0") with this javascript snippet in the question text (source code mode).
Here you select ten random numbers out of your ninety.
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 = [];
      for (var i = 1; i < 91; i++)
      {
         w=("00" + i).slice(-2); arr.push(w);
      }
 
      arr = shuffle(arr);
      anumbers = arr.slice(0,10).join(',');
      $('#question{QID} input[type="text"]').val("#"+anumbers);
      $('#question{QID}').hide();
      $('#ls-button-submit').trigger('click');      
   });
</script>

You will get something like
#12,02,54,78,23,66,13,45,76,28

Now the relevance equation of each pair of questions is "Does the string contain this number?"
Function "strpos"

Question A1: strpos(Q0,"01")>0
Question B1: strpos(Q0,"01")>0
Question A2: strpos(Q0,"02")>0
Question B2: strpos(Q0,"02")>0
...
Question A34: strpos(Q0,"34")>0
Question B34: strpos(Q0,"34")>0
...
Question A90: strpos(Q0,"90")>0
Question B90: strpos(Q0,"90")>0

All questions have the same randomization group name.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Moathh
  • Moathh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 3 months ago #213682 by Moathh
Thank you for your prompt response, you are a life savior! Just to double check, does this also take care of randomizing the questions so that the pairs will not be displayed right after each others?

Using the example you gave with #12,02,54,78,23,66,13,45,76,28, would the questions appear in consecutive pairs (i.e. A12, B12, A02, B02, etc.) or randomly (e.g. A12, B78, A02, B12, etc.)

Thank you again!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 3 months ago - 4 years 3 months ago #213686 by Joffm
Hi,
There are two answers to this question:

1. Did you try? 

2. Did you read the manuual about question types, especially this?
[url] www.limesurvey.org/manual/Question_type_...e_.28random_group.29 [/url]

All questions have the same randomization group name.


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 4 years 3 months ago by Joffm.
The topic has been locked.
  • Moathh
  • Moathh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 3 months ago #213699 by Moathh
Thank you again for replying and for the link. I tried the solution with a smaller set of questions (A1-4 and B1-4) and the randomization works perfectly, so many thanks for that! However, the only issue I faced when I previewed the survey is that when I want to choose 2 pairs out of these 4 pairs, sometimes I get 1 pair, sometimes I get exactly 2 as I wanted and sometimes 3 so I am not sure why this is happening..

I edited the code you provided as following:

<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 = [];
      for (var i = 1; i < 9; i++)
      {
         w=("00" + i).slice(-2); arr.push(w);
      }
 
      arr = shuffle(arr);
      anumbers = arr.slice(0,4).join(',');
      $('#question{QID} input[type="text"]').val("#"+anumbers);
      $('#question{QID}').hide();
      $('#ls-button-submit').trigger('click');      
   });
</script>

I am not sure if I edited the code properly..

Many thanks!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 3 months ago #213700 by Joffm
Hi,

    for (var i = 1; i < 9; i++)
      {
         w=("00" + i).slice(-2); arr.push(w);
      }

So you get a initial array of:
"01","02","03","04","05","06","07","08","09"
This is obviously not what you want to have; you only have 4 questions.

Well, this array with 9 elements is shuffled.
Then

  anumbers = arr.slice(0,4).join(',');

This means you select the first 4 elements out of these 9.
You might get something like
#08,01,07,06
But you only want to have 2.

So only one question will be displayed with the relevance equation strpos(Q0,"01")>0
There will be no questions with "strpos(Q0,"08")>0", or are there.

To correct your example with 2 out of 4 you  should set:

      for (var i = 1; i < 5; i++)
      {
         w=("00" + i).slice(-2); arr.push(w);
      }
 
      arr = shuffle(arr);
      anumbers = arr.slice(0,2).join(',');

So you get an initial array of "01","02","03","04"
and after shuffling and slicing two random elements like "#03,01"

Please, read some basics about this:
[url] www.w3schools.com/jsref/jsref_push.asp [/url]
[url] www.w3schools.com/jsref/jsref_slice_array.asp [/url]

Joffm



 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: Moathh
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose