How to not use the same randnumber twice

1 year 1 month ago - 1 year 1 month 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 year 1 month ago by CoeusProject.
The topic has been locked.
LimeSurvey Partners
1 year 1 month ago - 1 year 1 month 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++) {
			// Shuffle the numbers array
			// Load the first sub-question with the first number
			// Load the second sub-question with the second number
	// 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;

Sample survey attached:

File Attachment:

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

Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner -
Last edit: 1 year 1 month ago by tpartner.
The topic has been locked.

Start now!

Just create your account and start using Limesurvey today.

Register now