Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Search Results (Searched for: random)
- Joffm
Yesterday 18:30 - Yesterday 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
- Joffm
16 Apr 2024 19:58 - 16 Apr 2024 20:00
Replied by Joffm on topic Create random on the basis of answer option in a question
@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.
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
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
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
16 Apr 2024 09:41
Replied by Mazi on topic Create random on the basis of answer option in a question
@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?
E.g. once the first character was picked, how can you pass the updated letters to a second call of that function?
- rajkumar_dms
16 Apr 2024 05:10
Replied by rajkumar_dms on topic Create random on the basis of answer option in a question
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
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
15 Apr 2024 20:39
Replied by Joffm on topic Create random on the basis of answer option in a question
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.
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
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.
Here it is again.
Thanks.