Welcome to the LimeSurvey Community Forum

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

Randomize a specific number of attributes

  • Pat200
  • Pat200's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #210234 by Pat200
Hello everyone,

I hope you are good...

I tell you that I am with a survey where there is a question with 16 marks, this is not a problem because I asked a matrix question to show it, the problem is that I only have to show 8 random marks of the 16 that I have in the question.

Is there a way to do this within the same question, that is to say that the 8 marks are randomly displayed in the same question?

Or is there some other way to achieve this?

I really appreciate your help.

Sincerely.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #210246 by Joffm
Replied by Joffm on topic Randomize a specific number of attributes
You have to generate the random marks before.
Then you display them by subquestion relevance.
1. Create a question of type "short text". Let's call it Q0.
Enter this in the question text (source code mode)
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 <17; i++) {
         arr.push(i);
      }
 
      arr = shuffle(arr);
      anumbers = arr.slice(0,8).join(',');
      $('#question{QID} input[type="text"]').val("#,"+anumbers+",");
      $('#question{QID}').hide();
   });
</script>

You will get something like "#,12,5,3,11,15,6,4,10,"
2. Set the subquestion relvances to
1st item: strpos(Q0,",1,")>0
2nt item: strpos(Q0,",2,")>0
3rd item: strpos(Q0,",3,")>0
...
16th item: strpos(Q0,",16,")>0

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Pat200
  • Pat200's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #210434 by Pat200
Replied by Pat200 on topic Randomize a specific number of attributes
Thanks Joffm,

I think I have a problem, I don't know if it's something I'm doing wrong.

I created the question to put the script and use the relevancy equation to call these random marks.

the problem is that when I get to the question it doesn't show me any mark.

I am attaching a survey file in lss.

Thank you joffm for all your time.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #210436 by Joffm
Replied by Joffm on topic Randomize a specific number of attributes
Hi,
what you do wrong is rather obvious.
You display these two question on one screen (group by group).
In the script you see the line
Code:
$(document).on('ready pjax:scriptcomplete',function(){
So the script is executed after the page is loaded completely. Then it will not work.

So you have to put the question with the script into a different group - together with some other questions - before that array question 
Then you get (here I showed question Q0





Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose