Randomize a specific number of attributes

More
1 month 3 weeks 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.

Please Log in to join the conversation.

LimeSurvey Partners
More
1 month 3 weeks ago #210246 by Joffm
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)
<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

Please Log in to join the conversation.

More
1 month 2 weeks ago #210434 by Pat200
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.
Attachments:

Please Log in to join the conversation.

More
1 month 2 weeks ago #210436 by Joffm
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
$(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
Attachments:

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now