How to not use the same randnumber twice

More
1 month 3 weeks ago - 1 month 3 weeks ago #191506 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: 1 month 3 weeks ago by CoeusProject.

Please Log in or Create an account to join the conversation.

LimeSurvey Partners
More
1 month 3 weeks ago - 1 month 3 weeks ago #191558 by tpartner
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.

<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()) == '' && $.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.
Official LimeSurvey Partner - partnersurveys.com
Attachments:
Last edit: 1 month 3 weeks ago by tpartner.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!