Welcome to the LimeSurvey Community Forum

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

Randomization

  • CoeusProject
  • CoeusProject's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209281 by CoeusProject
Randomization was created by CoeusProject
Dear All,


I have a brief question and hope that someone can help me with it.


I want to give my respondents in different questions different pieces of information. So if Random=1 they see a certain sentence in the question, if random=2, they see a different sentence and so forth.

However, I want to make sure that they do not get in two questions the same pieces of information. Once the information linked to, for example, "Random 1" has been used, it cannot be shown in a different question again.

Is it possible to make sure that a random number cannot be reused again?

Thank you in advance for any help you can provide.

Martijn
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #209284 by Joffm
Replied by Joffm on topic Randomization
Hi,
you cannot be sure if you only generate some random numbers.

You should generate a randomized string of characters at the start of your survey, like afhibdfcge.
Now you may seklect the text that is assigned to "a" in your first question, that is assigned to "f" in your second question, and so on.

Use function "substr" to extract the 1st, 2nd, 3rd character
Read here:
manual.limesurvey.org/ExpressionScript_-...mplemented_functions

You generate this string in a question of type "short text" with this javascript snippet.
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; adapt to your needs
      var arr = ["a","b","c","d","e","f","g","h","i"];
      arr = shuffle(arr);
      anumbers = arr.join('');
      $('#question{QID} input[type="text"]').val(anumbers);
//      $('#question{QID}').hide();
   });
</script>

Remove the slashes to hide the question.
// $('#question{QID}').hide();

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: CoeusProject
The topic has been locked.
  • CoeusProject
  • CoeusProject's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209310 by CoeusProject
Replied by CoeusProject on topic Randomization
Dear Joffm,

Thank you for your prompt reply. This is a solution I wouldn't have come up with in a million years (I am not so skilled in programming languages).

The part in which I generate a string has worked very well. However, I am now already fighting for over an hour with the part of importing the selected part of the string to another question. I have followed the link you sent me and tried some of the examples on the website, but nothing seems to work.

Probably it is something very simple, when one knows what to do. How do I import the string I have created in the first question to the second question?

Best wishes,

Martijn
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #209315 by Joffm
Replied by Joffm on topic Randomization
Hi, Martijn,

This is a solution I wouldn't have come up with in a million years

I'm sure you would.;) This solution is presented many times here in the forum.

Well, what is your problem?
You want to display a random text in question 1, a different one in question 2, and so on.
You have a random string.
Let's call the question where you generate it "QDef" (=definition)

Now in your first question you take the first character of the string to define the text.
The first character you get by the function "substr(QDef,0,1)" (Take the substring of QDef, starting with 0 and a length of 1)
and the second by "substr(QDef,1,1)".
Remember: The index of the first character is "0".

And according to this character you display the text.
This is i simple nested IF-statement.

So your question text of the first question could be:

Now we want to ask you something.
{if(substr(QDef,0,1)=="a","This is the text when the first character is a",if(substr(QDef,0,1)=="b","This is the text when the first character is b", if(substr(QDef,0,1)=="c","This is the text when the first character is c","This is the text when the first character is something else")))}
And again some general text

So your question text of the second question could be: (here you capture the second character)

Now we want to ask you something.
{if(substr(QDef,1,1)=="a","This is the text when the second character is a",if(substr(QDef,1,1)=="b","This is the text when the second character is b", if(substr(QDef,1,1)=="c","This is the text when the secondd character is c","This is the text when the second character is something else")))}
And again some general text


Read about the syntax of the IF-statement (it's the same as in EXCEL) and nesting.

If there are more questions, please send your tries as lss export.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: CoeusProject
The topic has been locked.
  • CoeusProject
  • CoeusProject's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago - 3 years 4 months ago #209692 by CoeusProject
Replied by CoeusProject on topic Randomization
Thank you again, Joffm. This was very helpful. Once I knew what I had to look for I also found your other
post from last month about a similar problem and with the information of these two answers I could solve my problem
Last edit: 3 years 4 months ago by CoeusProject.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose