Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Search Results (Searched for: random)
- Orélie
23 Apr 2024 11:03 - 23 Apr 2024 11:05
Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey Cloud Version 6.5.4
Own server or LimeSurvey hosting: Limesurvey hosting
Survey theme/template: Fruity 23
==================
Hello,
I made a survey with 3 randomly displayed groups (see attached lss file).
I'd like to display the 1st group to 10% of the participants, the 2nd group and the 3rd groups to 45% of the participants.
Does anyone have the correct syntax?
Thanks a lot!
Assign a distribution percentage to randomly displayed groups was created by Orélie
Attachment not found
Your LimeSurvey version: LimeSurvey Cloud Version 6.5.4
Own server or LimeSurvey hosting: Limesurvey hosting
Survey theme/template: Fruity 23
==================
Hello,
I made a survey with 3 randomly displayed groups (see attached lss file).
I'd like to display the 1st group to 10% of the participants, the 2nd group and the 3rd groups to 45% of the participants.
Does anyone have the correct syntax?
Thanks a lot!
- rajkumar_dms
23 Apr 2024 10:44 - 23 Apr 2024 14:21
Is their is any compiler for the java script syntax was created by rajkumar_dms
Please help us help you and fill where relevant:
Your LimeSurvey version: [6.5]
Own server or LimeSurvey hosting:Clould
Survey theme/template:
==================
I would like the below compile the below syntax in lime survey, can i compile it
<script type="text/javascript">
$(document).on('ready pjax:scriptcomplete',function(){
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array, array[j]] = [array[j], array];
}
return array;
}
// Example: Customizing rotation for a multiple choice question
$(document).ready(function() {
// Identify the question by its ID or class
var questionId = $('#question{QID}'); // Change to the appropriate ID or class
// Get the answer choices container
var answerChoicesContainer = $(questionId).find('.answer-choices');
// Get the answer choices
var answerChoices = answerChoicesContainer.children('.answer-item');
// Array to store fixed elements
var fixedElements = [2,4,6];
// Loop through answer choices to find fixed elements
answerChoices.each(function() {
if ($(this).hasClass('fixed')) { // Assuming 'fixed' is a class added to fixed elements
fixedElements.push($(this));
}
});
// Remove fixed elements from the answer choices array
fixedElements.forEach(function(element) {
answerChoices = answerChoices.not(element);
});
// Shuffle the remaining answer choices
var shuffledChoices = shuffleArray(answerChoices);
// Insert fixed elements back into the shuffled choices array
fixedElements.forEach(function(element) {
shuffledChoices.splice(element.data('position'), 0, element); // Assuming 'position' is a data attribute indicating the fixed position
});
// Append the shuffled choices back to the container
answerChoicesContainer.empty().append(shuffledChoices);
});
</script>
Your LimeSurvey version: [6.5]
Own server or LimeSurvey hosting:Clould
Survey theme/template:
==================
I would like the below compile the below syntax in lime survey, can i compile it
<script type="text/javascript">
$(document).on('ready pjax:scriptcomplete',function(){
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array, array[j]] = [array[j], array];
}
return array;
}
// Example: Customizing rotation for a multiple choice question
$(document).ready(function() {
// Identify the question by its ID or class
var questionId = $('#question{QID}'); // Change to the appropriate ID or class
// Get the answer choices container
var answerChoicesContainer = $(questionId).find('.answer-choices');
// Get the answer choices
var answerChoices = answerChoicesContainer.children('.answer-item');
// Array to store fixed elements
var fixedElements = [2,4,6];
// Loop through answer choices to find fixed elements
answerChoices.each(function() {
if ($(this).hasClass('fixed')) { // Assuming 'fixed' is a class added to fixed elements
fixedElements.push($(this));
}
});
// Remove fixed elements from the answer choices array
fixedElements.forEach(function(element) {
answerChoices = answerChoices.not(element);
});
// Shuffle the remaining answer choices
var shuffledChoices = shuffleArray(answerChoices);
// Insert fixed elements back into the shuffled choices array
fixedElements.forEach(function(element) {
shuffledChoices.splice(element.data('position'), 0, element); // Assuming 'position' is a data attribute indicating the fixed position
});
// Append the shuffled choices back to the container
answerChoicesContainer.empty().append(shuffledChoices);
});
</script>
- rajkumar_dms
22 Apr 2024 19:54
Replied by rajkumar_dms on topic Create random on the basis of answer option in a question
Thanks joffm
You are the genius joffm
Thank you
You are the genius joffm
Thank you
- DenisChenu
22 Apr 2024 10:52
Replied by DenisChenu on topic Distribuer aléatoirement le participants dans 3 groupes de questions parmi 30
Le plus simple selon ce que tu expliques me semble d'utiliser plutôt la rotation des groupes, à tester
> Cependant, je ne parviens pas à appliquer l'équation conditionnelle "random == 1 AND random2 == 1" aux groupes de métiers, car cela entraîne un dysfonctionnement complet du questionnaire.
Qu'est ce qui ne fonctionne pas ? C'ets pas clair du tout.
- Tu places les groupes dans le même « Nom de groupe aléatoire »
- Tu ajoutes dans chaque groupe une question de type équation avec 1 comme formule. EQG1, EQG2,EQG3 etc …
- Tu ajoutes la condition : sum(EQG1.NAOK, EQG2.NAOK,EQG3.NAOK…) < 3 OR EQG1.NAOK sur chaque groupe (le dezrnier EQG1 est à modifier pour chaque groupe)
- Tu test
> Cependant, je ne parviens pas à appliquer l'équation conditionnelle "random == 1 AND random2 == 1" aux groupes de métiers, car cela entraîne un dysfonctionnement complet du questionnaire.
Qu'est ce qui ne fonctionne pas ? C'ets pas clair du tout.
- Joffm
21 Apr 2024 14:00
Replied by Joffm on topic Create random on the basis of answer option in a question
Exact.
N x 3 + 1 (for final)
Joffm
N x 3 + 1 (for final)
Joffm
- Mazi
21 Apr 2024 13:06
Replied by Mazi on topic Create random on the basis of answer option in a question
Thanks a lot for this detailed explanation, joffm!
So If I get this right, when extending this from top3 to top5 we will end up with 16 RC sub-questions?!
So If I get this right, when extending this from top3 to top5 we will end up with 16 RC sub-questions?!
- Joffm
19 Apr 2024 18:30 - 19 Apr 2024 18:32
Replied by Joffm on topic Create random on the basis of answer option in a question
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
Hide the question with css class "d-none"
and use the same subquestion conditions.
Joffm
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
- rajkumar_dms
18 Apr 2024 20:14
Replied by rajkumar_dms on topic Create random on the basis of answer option in a question
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
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
- holch
18 Apr 2024 20:01
Replied by holch on topic Create random on the basis of answer option in a question
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.
This is a forum where users help users. There is no right for a (quick) answer. Please be patient.
- rajkumar_dms
18 Apr 2024 07:25
Replied by rajkumar_dms on topic Create random on the basis of answer option in a question
Hi,
Can you share the example for the same as per my wish
Can you share the example for the same as per my wish
- tpartner
17 Apr 2024 18:49
Replied by tpartner on topic Customise item rotation in limesurvey matrix question
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.
Sample survey:
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:
- BBSR-SR5
17 Apr 2024 18:22
Replied by BBSR-SR5 on topic Customise item rotation in limesurvey matrix question
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()
How would I access the participant number from the code?
Though how would I access that? Right now I've plugged in Date.now()
Code:
<script type="text/javascript" data-author="BBSR-SR5 & 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
17 Apr 2024 14:58 - 17 Apr 2024 15:10
Replied by BBSR-SR5 on topic Sonstiges Option Klickbar machen ohne Texteingabe (Optionale Texteingabe)
Ich habe die Lösung selber gefunden. In dem fall aktiviert man einfach die Zufällig Antwortrotation und ergänzt folgenden Code im Fragefenster:
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!
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 && $('#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
17 Apr 2024 11:38
Replied by BBSR-SR5 on topic Sonstiges Option Klickbar machen ohne Texteingabe (Optionale Texteingabe)
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:
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
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>
- rajkumar_dms
16 Apr 2024 20:11 - 17 Apr 2024 09:01
Replied by rajkumar_dms on topic Create random on the basis of answer option in a question