Welcome to the LimeSurvey Community Forum

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

Randomization without replacement

  • Rayymetch_328054
  • Rayymetch_328054's Avatar Topic Author
  • Offline
  • Banned
  • Banned
More
3 years 4 months ago - 3 years 4 months ago #224165 by Rayymetch_328054
Randomization without replacement was created by Rayymetch_328054
Please help us help you and fill where relevant:
Your LimeSurvey version: 3.17.7+190627
Own server or LimeSurvey hosting: university
Theme:  student research
==================

Hi everyone!

I have some trouble finding the right solution for a randomization issue:

The goal is to get 5 hidden questions containing a random number between 1 and 5 with the additional condition that among these 5 questions every number can only occur=12.0pt once.
While getting random numbers seems to be quite easy, I do not know how to implement my additional condition.

To get the random number this works just fine: {if(is_empty(RandomPosition1), rand(1, 5), RandomPosition1)} however I am in need for some advice regarding my additional condition.

Thanks in advance!
 
Last edit: 3 years 4 months ago by Rayymetch_328054.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #224171 by Joffm
Replied by Joffm on topic Randomization without replacement
Hi,
this is nothing else than to have the number 1,2,3,4,5 in randomized order. (if I translate this "can only occur=12.0pt once" correctly)
This you can achieve easily with a small javascript snippet.
Create a question of type "multiple short text" with 5 subquestions and insert this snippet into the source code
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 arr = [];
      for (var i = 1; i < 6; i++) {
         arr.push(i);
      }
 
      arr = shuffle(arr);
      $('#question{QID} input[type="text"]:eq(0)').val(arr[0]);
      $('#question{QID} input[type="text"]:eq(1)').val(arr[1]);
      $('#question{QID} input[type="text"]:eq(2)').val(arr[2]);
      $('#question{QID} input[type="text"]:eq(3)').val(arr[3]);
      $('#question{QID} input[type="text"]:eq(4)').val(arr[4]);
//    After testing remove the slashes of the next row to hide this question
//    $('#question{QID}').hide();
   });
</script>
You will get 5 unique numbers between 1 and 5
 


But first make sure that you have the rights to insert javascript (I see "university" installation)
To check, insert this in the source code of a question
Code:
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        alert("Hi, here is javascript");
    });
</script> 

But you should explain a bit more what you want to achieve. Maybe we can advise a better solution.

Here - in the German part - is a solution without javascript, only some equations (See 2. Das "Ziehen ohne Zurücklegen" nachgestellt.)
[url] forums.limesurvey.org/forum/german-forum...zufallszahlen#209705 [/url]

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Rayymetch_328054
  • Rayymetch_328054's Avatar Topic Author
  • Offline
  • Banned
  • Banned
More
3 years 4 months ago - 3 years 4 months ago #224371 by Rayymetch_328054
Replied by Rayymetch_328054 on topic Randomization without replacement
Thanks a lot Joffm. Javascript is working with my installation.

One additional question to your Javascript solution: The script perfectly gives me randomized numbers but I want to use the numbers to specify further conditions. However my conditions do not work unless I press enter (or anything else) in one of the 5 subquestions of the Javascript. Is there any solution for that? My goal is to hide the Javascript question. I attached a .lss document for visualisation.

Thanks in advance!
Last edit: 3 years 4 months ago by Rayymetch_328054.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago - 3 years 4 months ago #224372 by Joffm
Replied by Joffm on topic Randomization without replacement
Hi,
nearly everything is fine, except:
1. You want to display "question by question".
This is not advisable.
The script cannot be hidden by "Always hide this question", but by javascript.
Well, you can run the script and at the end trigger the "Next"- button. But it will cause an unpleasant flicker of the page.
Therefore we recommend to display "group by group" and put one question into each group.
This way you have more options:
Fake the display "question by question", in some cases display two or more questions on on page, do some manipulations using javascript.
 
2. The question with the javascript must not be on the same page. Put it into a different group at the beginning of the survey.
As you see this line

    $(document).on('ready pjax:scriptcomplete',function(){

the script starts after the document is loaded (ready).
Meaning: Your question "question" is displayed before the script works.

3. To hide it
There are these to lines in the script//    After testing remove the slashes of the next row to hide this question
//    $('#question{QID}').hide();So, do it: remove the slashes to hide the question.

Here two sample survey with both options
"question by question"
 

File Attachment:

File Name: limesurvey...2857.lss
File Size:21 KB


"group by group"
 

File Attachment:

File Name: limesurvey...1279.lss
File Size:22 KB


Joffm

By the way:
Your first requirement was

The goal is to get 5 hidden questions containing a random number between 1 and 5

This you got. Not more.
You did not say what you want to use it for.
If you tell us a bit more there may be a better solution to aim your goal.

Always remember: Help us to help you.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 4 months ago by Joffm.
The following user(s) said Thank You: Rayymetch_328054
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago - 3 years 4 months ago #224379 by Joffm
Replied by Joffm on topic Randomization without replacement
Furthermore you may use the example in the German part (without javascript, only equations)
So there are no restrictions about "question by question" vs. "group by group", etc.

Here a sample survey for your requirements
 

File Attachment:

File Name: limesurvey...9_JK.lss
File Size:26 KB


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 4 months ago by Joffm.
The following user(s) said Thank You: Rayymetch_328054
The topic has been locked.
  • Rayymetch_328054
  • Rayymetch_328054's Avatar Topic Author
  • Offline
  • Banned
  • Banned
More
3 years 4 months ago #224381 by Rayymetch_328054
Replied by Rayymetch_328054 on topic Randomization without replacement
Thanks a lot Joffm! That was very helpful!
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose