Welcome to the LimeSurvey Community Forum

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

Search Results (Searched for: random)

  • Mazi
  • Mazi's Avatar
Today 09:41
@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?
  • rajkumar_dms
  • rajkumar_dms's Avatar
Today 05:10
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
  • Joffm
  • Joffm's Avatar
Yesterday 20:39
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.

​​​​​​
  • Maddie.Storm
  • Maddie.Storm's Avatar
Yesterday 15:42
Replied by Maddie.Storm on topic Randomization 50:50
Sorry, accidentally replied with the wrong account if there was confusion.

Here it is again. 
Thanks.
  • PabloSantos
  • PabloSantos's Avatar
Yesterday 15:34 - Yesterday 15:44
Replied by PabloSantos on topic Randomization 50:50
Hi!
I am so sorry about that. I attached the lss file here if you want to take a look! It should work.
 
  • rajkumar_dms
  • rajkumar_dms's Avatar
Yesterday 13:06
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
  • Mazi
  • Mazi's Avatar
Yesterday 12:54
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.
  • rajkumar_dms
  • rajkumar_dms's Avatar
Yesterday 08:17 - Yesterday 08:18
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

 
  • Joffm
  • Joffm's Avatar
14 Apr 2024 02:11
Replied by Joffm on topic Randomization 50:50
Please provide neither lsq nor lsg exports.
We always ask for lss exports of the relevant questions.

Reason:
lsq and lsg exports are language sensitive.
You can't import them into a survey with a different base language 
So first we have to guess the base language, create a survey to be able to import.
Furthermore your survey wide settings are missing.

lss exports we only have to import.

Please, ease the work of us volunteers.

Joffm
  • Maddie.Storm
  • Maddie.Storm's Avatar
13 Apr 2024 23:42
Replied by Maddie.Storm on topic Randomization 50:50
Sure thing!
Here is the question group attached.

Thank you SO much.
  • Joffm
  • Joffm's Avatar
13 Apr 2024 20:55 - 13 Apr 2024 21:25
Replied by Joffm on topic Randomization 50:50
Better you attach the lss export of your try.

As @holch wrote you create the random number in a question of type "equation" which is called "randnumber". 
Please read the manual about question types.
 Then you enter the shown logical terms (randnumber==1 resp  randnumber==2) in the "condition" field of the question.

And why don't you see a 50:50 distribution?
What do you expect if you throw a coin 100 times? This is analogue to the creation of a random number from 1-2 100 times.

Again my advice. Read the manual, study the included sample surveys to learn the basics about questions, about ExpressionScript, about the implemented functions, etc.

Joffm
  • Maddie.Storm
  • Maddie.Storm's Avatar
13 Apr 2024 18:30
Replied by Maddie.Storm on topic Randomization 50:50
Hi!
I attached pictures here... I know it seems like a mess! 
Would you be able to provide me the exact code to put in these? I am not the best at this.. I am just looking for 50:50 between variables 1 and 2. 
What would be the exact condition for my first question (variable 1) in the group and second question (variable 2)? It does not seem to pick up on randnumber == 1?
Then I tried filling them in and it doesn't seem to work either.

Each Condition for ModifiedGMSEX (1) and FullyModifiedGMSEX (2): ?

Exact Group Relevance Code (Modified GMSEX): ?

Randomization group name: ?


Thank you so much for your help! 
  • Joffm
  • Joffm's Avatar
13 Apr 2024 16:09
Replied by Joffm on topic Randomization 50:50
Hi,
it seems you are confused about it.

You see the construct we use
Code:
{if(is_empty(randnumber), rand(1,2),randnumber)}
Meaning: if "randnumber" is still empty, create a random number from 1-2, otherwise keep it as it is.

Reason for this construct:
If you only use {rand(1,2)} you create a random number. But if the respondent goes back to this question or even group there will be generated a new random number that may be different.
Therefore the check "is_empty". 
​​​​​​

Well, now yoa have in your variable "randnumber" a value (1 or 2).

And now the conditions of your questions.
One question is only displayed if this "randnumber" is equal 1, the other if it is equal 2.

And exactly this you enter into the condition of the question.
randnumber==1 resp. randnumber==2

Best you read the manual, "ExpressionScript" again and you may download and import the sample surveys provided in the manual.

Joffm 
  • Maddie.Storm
  • Maddie.Storm's Avatar
13 Apr 2024 15:42
Replied by Maddie.Storm on topic Randomization 50:50
Hi! Thank you so much for responding. I just tried to do it, but I think I still have a problem. My questions are in array form and they have to stay that way. It is for the scale validity in our study, and it seems like the equation style would not work for the length and look of the array.
So, is there any way to randomize arrays 50:50? Is there a code for these? Where do I put each code?

Also, how do I do this: "You need to check if this random number already exists before you create it new."? How do I assign one question as 1 and the other as 2? Does this go into the condition box?

Thanks so much! I am very new to LimeSurvey. 
  • tpartner
  • tpartner's Avatar
12 Apr 2024 14:06
This should do it:

Code:
<script type="text/javascript">
    $(document).on('ready pjax:scriptcomplete',function(){
 
        function shuffleArray(array) {
            for (var i = array.length - 1; i > 0; i--) {
                var j = Math.floor(Math.random() * (i + 1));
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
            return array;
        }
 
        // Merge function
        function mergeArrays(array1, array2) {
            var mergedArray = [];
            var maxLength = Math.max(array1.length, array2.length);
 
            for (var i = 0; i < maxLength; i++) {
                if (i < array2.length) {
                    mergedArray.push(array2[i]);
                }
                if (i < array1.length) {
                    mergedArray.push(array1[i]);
                }
            }
 
            return mergedArray;
        }
 
        //Identify this question
        var thisQuestion = $('#question{QID}');
        var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent();
        var thisTable = $('table.subquestion-list:eq(0)', thisQuestion);
 
        // Special codes appear after
        var afterCode = 4;
 
        //Array for K Codes
        var array_k = ["b","d","g","h","i","j","l"];
 
        //Array for N Codes
        var array_n = ["c","e","f","k","m","n"];
 
        //Array for Special K Codes
        var array_sk = [];
 
        //Array for Special N codes
        var array_sn = ["a"];
 
        //Array for super special codes
        var array_ss = ["p"];
 
        // Shuffle order of "normal" arrays
        shuffleArray(array_k);
        shuffleArray(array_n);
 
        // Slice "normal" arrays and merge second part to special codes
        array_sk = array_sk.concat(array_k.slice(afterCode));
        array_sn = array_sn.concat(array_n.slice(afterCode));
 
        // Save only first part of "normal" arrays
        array_k = array_k.slice(0, afterCode);
        array_n = array_n.slice(0, afterCode);
 
        // Shuffle special arrays
        shuffleArray(array_sk);
        shuffleArray(array_sn);
        shuffleArray(array_ss);
 
        // Combine normal and special arrays
        array_k = array_k.concat(array_sk);
        array_n = array_n.concat(array_sn);
 
        // Merge arrays in order n,k,n,k...
        var array_res = mergeArrays(array_k, array_n);
 
        //Add super special array to end of result array
        array_res = array_res.concat(array_ss);
 
        // Loop through the answer codes
        $.each(array_res, function(i, val) {
            // Move the answer item
            $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion));
        });
 
        // Fix up the row background colours
        $('tr.answers-list', thisQuestion). each (function(i){
            $(this).removeClass('ls-even ls-odd');
            if(i % 2 == 0) {
                $(this).addClass('ls-even');
            }
            else {
                $(this).addClass('ls-odd');
            }
        });
 
    });  
 
</script>

Sample survey: 

File Attachment:

File Name: limesurvey...3181.lss
File Size:41 KB
Displaying 1 - 15 out of 664 results.

Lime-years ahead

Online-surveys for every purse and purpose