Welcome to the LimeSurvey Community Forum

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

Search Results (Searched for: random)

  • rajkumar_dms
  • rajkumar_dms's Avatar
Today 07:25
Hi,

Can you share the example for the same as per my wish
  • tpartner
  • tpartner's Avatar
Yesterday 18:49
My solution would be to do as Joffm suggests - load the sub-question order in a hidden (via CSS class) short-text question. On page load, if that question is found to be populated, that order is used, otherwise your randomization alogorithm kick in.

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 some elements and define variables
        var thisQuestion = $('#question{QID}');
        var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent();
        var thisTable = $('table.subquestion-list:eq(0)', thisQuestion);
        var orderQuestion = $(thisQuestion).nextAll('.text-short:eq(0)');
        var orderInput = $(':text.form-control:eq(0)', orderQuestion);
        var array_res;
        var aOrder = [];
 
        // Previously set order
        if($.trim($(orderInput).val()) != '') {
            array_res = $.trim($(orderInput).val()).split(',');
        }
        // No previous order set
        else {    
            // 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...
            array_res = mergeArrays(array_k, array_n);
 
            //Add super special array to end of result array
            array_res = array_res.concat(array_ss);
 
            // Load the hidden order input
            $(orderInput).val(array_res);
        }
 
        // 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...3182.lss
File Size:56 KB
  • BBSR-SR5
  • BBSR-SR5's Avatar
Yesterday 18:22
Hm actually, I figured it might be easier to replace the Math.random() function with a seeded random function and then use the survey number as a seed. That way it should stay constant for a user.

Though how would I access that? Right now I've plugged in Date.now()
Code:
<script type="text/javascript" data-author="BBSR-SR5 &amp; Tony Partner">
  $(document).on('ready pjax:scriptcomplete',function(){
 
 
//Define Seeded Random function
   function splitmix32(a) {
  return function() {
    a |= 0;
    a = a + 0x9e3779b9 | 0;
    let t = a ^ a >>> 16;
    t = Math.imul(t, 0x21f0aaad);
    t = t ^ t >>> 15;
    t = Math.imul(t, 0x735a2d97);
    return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
  };
}
 
function shuffleArray(array) {
  const seed = Date.now();
  const random = splitmix32(seed);
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  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 = ["o"];
 
    // 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');
            }
        });
 
      //alert(array_res);
    //console.log(array_res);
    });
 
 
 
</script>

How would I access the participant number from the code?
  • BBSR-SR5
  • BBSR-SR5's Avatar
Yesterday 14:58 - Yesterday 15:10
Ich habe die Lösung selber gefunden. In dem fall aktiviert man einfach die Zufällig Antwortrotation und ergänzt folgenden Code im Fragefenster:
Code:
<script type="text/javascript" charset="utf-8">
 
$(document).on('ready pjax:scriptcomplete',function(){
 
// The number of answers to be fixed at the end of the list
var fixedAnswers = 1;
 
// Set this to "true" if you want "Other" to be fixed in the last position
var otherFixed = false;
 
// Identify this question
var qID = {QID};
 
// Find the number of answers
var ansCount = $('#question'+qID+' .answer-item').length;
if($('#question'+qID+' input[type="text"]').length > 0) {
ansCount = ansCount -1
}
console.log(ansCount);
 
// Place the last n answers created at the end of the list
var fixedIndex = fixedAnswers - 1;
for (var i=0; i<fixedAnswers; i++) {
var answer = $('input[id^="answer"][id$="X'+qID+(ansCount-fixedIndex)+'"]');
var answerItem = $(answer).closest('.answer-item');
var answersList = $(answer).closest('ul');
$(answersList).append(answerItem);
fixedIndex--;
}
 
// Handle "Other"
if(otherFixed == true &amp;&amp; $('#question'+qID+' input[type="text"]').length > 0) {
var otherAnswer = $('#question'+qID+' input[type="text"]');
var otherAnswerItem = $(otherAnswer ).closest('.answer-item');
var otherAnswersList = $(otherAnswer ).closest('ul');
$(otherAnswersList).append(otherAnswerItem);
}
});
</script>


Den Code habe ich aus dem manual hier btw: manual.limesurvey.org/Workarounds:_Manip...28radio.29_questions

Edit: Wichtiger Hinweis, der Code funktioniert nur wenn die Antwortoptionen mit Zahlen nummeriert sind.
1,2,3,4,5,6 geht
a,b,c,d,e,f geht nicht!
  • BBSR-SR5
  • BBSR-SR5's Avatar
Yesterday 11:38
Danke für den Hinweis,

das habe ich jetzt so umgesetzt. Ich habe jetzt nur das Problem, dass ich gerne die Items rotieren möchte und das sonstige Item an seinem (letzten) Platz lassen möchte. Ich hatte gehofft, dass ich dafür einfach den Code aus den Matrix rotationen verwenden könnte:
Code:
<script type="text/javascript" data-author="Tony Partner">
  $(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;
}
    
    
    //Identify this question
    var thisQuestion = $('#question{QID}');
    var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent();
 
    // Special codes appear after
    var afterCode=11;
    
    // Fill the array with "normal" codes
    var array1=[1,2,3,4,5,6,7,8,9,10,11];
    
    // Fill the array with "special" codes
    var array2=[12];
    shuffleArray(array1);
    array2=array2.concat(array1.slice(afterCode));
    array1=array1.slice(0,afterCode);
    shuffleArray(array2);
    array1=array1.concat(array2);
    
    alert(array1)
 
    // Loop through the answer codes
    $.each(array1, function(i, val) {
      // Move the answer item
      $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion));
    });
  });
</script>
Aber das funktioniert nicht. Während der array1 korrekt erzeugt wird, wird die Anordnung der Items nicht verändert. Ich nehme an, der Loop am Schluss muss dafür verändert werden, aber ich weiß nicht wie :)
  • rajkumar_dms
  • rajkumar_dms's Avatar
16 Apr 2024 20:11 - Yesterday 09:01
 
  • Joffm
  • Joffm's Avatar
16 Apr 2024 19:58 - 16 Apr 2024 20:00
@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. 
  • Joffm
  • Joffm's Avatar
16 Apr 2024 17:16
Replied by Joffm on topic Randomization 50:50
Hi,
​​​​​​in your survey I see nothing of the things @holch and I told you.

Where is the question of type "equation", where you generate the random number as said and shown before?
Best you put it into the "Sociodemographic" group.

And where are the two conditions
"randnumber==1" and "randnumber==2" in the questions "EndMod" resp. "FullyMod"?

That's all you have todo.

By the way you should use a quota to screen out people who do not consent.
Instead of putting this condition into each group condition.

Joffm 
  • Mazi
  • Mazi's Avatar
16 Apr 2024 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
16 Apr 2024 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
15 Apr 2024 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
15 Apr 2024 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
15 Apr 2024 15:34 - 15 Apr 2024 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
15 Apr 2024 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
15 Apr 2024 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.
Displaying 1 - 15 out of 655 results.

Lime-years ahead

Online-surveys for every purse and purpose