Welcome to the LimeSurvey Community Forum

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

How to not use the same randnumber twice

  • CoeusProject
  • CoeusProject's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 4 months ago - 4 years 4 months ago #191506 by CoeusProject
How to not use the same randnumber twice was created by CoeusProject
Dear All:

I am struggling with an issue and I am wondering whether anybody has a good idea how to solve it.

I want to ask my respondents a question after showing them a brief text. It is a survey experiment so not all respondents receive the same information. A randnumber will decide what text they get to see.

Later in the survey, they will receive the same question again, but I want to make sure that they do not get the same information they have received before the first question.

Do you know whether there is a way to "exclude" numbers from a randnumber once they have been used before to make sure that they are not repeated?

Thank you for any help you can provide.
Last edit: 4 years 4 months ago by CoeusProject.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 4 months ago - 4 years 4 months ago #191558 by tpartner
Replied by tpartner on topic How to not use the same randnumber twice
I don't know of a way to do it with Expression Manager but here is a JavaScript solution.

Create multiple-short-text question with two sub-questions.

Add this script to the source of that question. Adjust the "minNumber" and "maxNumber" variables as necessary. The script will create an array of possible numbers, randomize it and then load the first two numbers into the sub-questions. You can then base relevance on those sub-questions.

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).on('ready pjax:scriptcomplete',function(){  
 
    // Identify this question
    var thisQuestion = $('#question{QID}');      
    var input1 = $('input:text:eq(0)', thisQuestion);
    var input2 = $('input:text:eq(1)', thisQuestion);
 
    // Define the minimum and maximum numbers
    var minNumber = 1;
    var maxNumber = 10;
 
    // Only load random numbers once
    if($.trim($(input1).val()) == '' &amp;&amp; $.trim($(input2).val()) == '') {
 
      // Create an array of possible numbers
      var numbers = [];
      var i;
      for (i = minNumber; i <= maxNumber; i++) {
        numbers.push(i);
      }
 
      // Shuffle the numbers array
      shuffleArray(numbers)
 
      // Load the first sub-question with the first number
      $(input1).val(numbers[0]).trigger('keyup');
 
      // Load the second sub-question with the second number
      $(input2).val(numbers[1]).trigger('keyup');
    }
 
 
  });
 
  // A function to shuffle arrays
  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;
  }  
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...7143.lss
File Size:19 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 4 months ago by tpartner.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose