Welcome to the LimeSurvey Community Forum

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

Create random on the basis of answer option in a question

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 months 6 days ago - 7 months 6 days ago #260314 by rajkumar_dms
Please help us help you and fill where relevant:
Your LimeSurvey version: 6.5
Own server or LimeSurvey hosting:Cloud
Survey theme/template:
==================
Hello Team,
Good day!
I would like create the random number on basis selected answer of previous question and then only top 3 will selected form that list

For example
I have a question Q1(Multiple choice) having answer option A,B,C,D,E,F,G,H,I AND i have selected option A,C,D,F,G,H,I, for another question Q2(Multiple choice) having same option as Q1 but i need to show the only top 3 option by randomly from Q1

 
Last edit: 7 months 6 days ago by rajkumar_dms.

Please Log in to join the conversation.

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
7 months 6 days ago #260332 by Mazi
Sounds tricky!
I think a solution can be to use array filter feature to only show the selected items at Q2.
Q2 should be set to be ordered randomly.
You can then apply some custom JS to remove all items after the top three.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 months 6 days ago #260333 by rajkumar_dms
I aware of the array filter that will show only the selected answer in previous selected answer
But my concern is it will show the randomly only top 3
Can you help on this by custom java script

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 months 5 days ago #260359 by Joffm
What are the "top 3" in a multiple question?
Or only 3 randomly selected from all selections?
For the second there are many examples in the forum, only using ExpressionScript with the idea:
1.
​​​​​​Create a string with symbols of the selected options like "BDEGI"
2. Create a random number from 1 to the length of this string.
3. Capture the letter at this position
4. Remove the letter from the string.
5. Start again at 2. now with the shorter string

Stop after you captured three letters.
Now you have your thrree randomly selected options 
Display the options that are represented by the letters with " conditions".

Search the forum for a more detailed description and a sample.
I am sure there is.

​​​​​​

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 months 5 days ago #260386 by rajkumar_dms
Thanks you for response
For the second i had the java script for the same
<script type="text/javascript" charset="utf-8">
    
    var numberToShow = 5;
    
    $(document).on('ready', function(){      
        if($('#question{QID} input:checkbox:checked').length == 0) {
            $('#question{QID} input:checkbox:lt('+numberToShow+')').each(function(i) {
                $(this).nextAll('input:hidden').val('Y');
                $(this).prop('checked', true).trigger('change');
            }); 
        }
    });
</script>
But my requirement something different i will show the 3 answer option randomly on the basis previous questions selected answer 
Let us suppose i have Q1 have 10 option but i selected the 6 option, from this 6 selected option we need to show only 3 option in the next question on random basis
I hope you understood my requierment

Please Log in to join the conversation.

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
7 months 5 days ago #260403 by Mazi
@joffm, that letter-based approach sounds pretty interesting. But how can you loop through this using EM?
E.g. once the first character was picked, how can you pass the updated letters to a second call of that function?

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 months 4 days ago - 7 months 4 days ago #260461 by Joffm
@Mazi,
at the moment I'm on vacation.
When I'm back in Germany I show/send a full example.

@rajkumar_dms, yes we understood your wish.
You have solution with pure ExpressionScript.

javascript is also possible.
Store the "string" in a question of type "short text".
Change the string to an array, shuffle the array, and use the first three elements. 

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

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 months 4 days ago - 7 months 4 days ago #260464 by rajkumar_dms
 
Last edit: 7 months 4 days ago by rajkumar_dms.

Please Log in to join the conversation.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 months 3 days ago #260536 by rajkumar_dms
Hi,

Can you share the example for the same as per my wish

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 months 2 days ago #260554 by holch
What part of "He is on vacation and he will share it when he is back in Germany" did you not understand?

This is a forum where users help users. There is no right for a (quick) answer. Please be patient.

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.

  • rajkumar_dms
  • rajkumar_dms's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 months 2 days ago #260555 by rajkumar_dms
Hi Holch,
Thanks for reply
Sorry for inconvenience
I doesn't mean for quick response,i just follow up the same
I thought he came back from vacation

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 months 1 day ago - 7 months 1 day ago #260597 by Joffm
Hi,
Let's say the first multiple question is Q1 with subquestion codes 1, 2, 3, ...
To do this I prefer to create a "container" to store interim results
1. Create a question of type "multiple short text" (I called it "RC" like "Result Container") with subquestions
RC_11
RC_12
RC_13
RC_21
RC_22
RC_23
RC_31
RC_32
RC_33
RC_99 (for the final result)
Three subquestions are needed for each element

2. Create a question of type "equation" and enter (only) the equations
First create the base:
{RC_11=join(if(Q1_1=="Y","A",""),if(Q1_2=="Y","B",""),if(Q1_3=="Y","C",""),if(Q1_4=="Y","D",""),if(Q1_5=="Y","E",""),if(Q1_6=="Y","F",""),if(Q1_7=="Y","G",""),if(Q1_8=="Y","H",""),if(Q1_9=="Y","I",""),if(Q1_10=="Y","J",""))}
We get something like "ADEGIJ"
Now the procedure I explained before
Random number from 1 to the length of this string
{RC_12=if(is_empty(RC_12),rand(1,strlen(RC_11)),RC_12)}

Capture the letter at this position
{RC_13=substr(RC_11,RC_12-1,1)}

Remove the letter at this position; this is the base for the next round
{RC_21=str_replace(RC_13,'',RC_11)}
{RC_22=if(is_empty(RC_22),rand(1,strlen(RC_21)),RC_22)}
{RC_23=substr(RC_21,RC_22-1,1)}


{RC_31=str_replace(RC_23,'',RC_21)}
{RC_32=if(is_empty(RC_32),rand(1,strlen(RC_31)),RC_32)}
{RC_33=substr(RC_31,RC_32-1,1)}


Now there are three letters (in RC_13, RC_23 and RC_33)

Joined with a  '#' (important for the function "strpos(x,y)")
{RC_99=join('#',RC_13,RC_23,RC_33)}

Now the conditions of the subquestions in Q2 are (just ask: Does the final strinf contain the letter)
strpos(RC_99,"A")>0
strpos(RC_99,"B")>0
strpos(RC_99,"C")>0

...


And there is this javascript solution.
Create a question of type "short text"
in the default answer enter
{list(if(Q1_1=="Y","A",""),if(Q1_2=="Y","B",""),if(Q1_3=="Y","C",""),if(Q1_4=="Y","D",""),if(Q1_5=="Y","E",""),if(Q1_6=="Y","F",""),if(Q1_7=="Y","G",""),if(Q1_8=="Y","H",""),if(Q1_9=="Y","I",""),if(Q1_10=="Y","J",""))}

and this script
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 str = $('#question{QID} input[type="text"]').val();
  var arr=str.split(", ");
  arr = shuffle(arr).slice(0,3).join('');
  $('#question{QID} input[type="text"]').val('#' + arr);
});
</script>


Hide the question with css class "d-none"
and use the same subquestion conditions.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 7 months 1 day ago by Joffm.

Please Log in to join the conversation.

Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose