Welcome to the LimeSurvey Community Forum

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

Pick random number from bucket & randomly assign to 2 different orders of quest

  • CarmenCampagnolo
  • CarmenCampagnolo's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 3 months ago #232853 by CarmenCampagnolo
Please help us help you and fill where relevant:
Your LimeSurvey version: [Version 3.22.4+200212]
Own server or LimeSurvey hosting:
Survey theme/template:
==================
Hi all,

1. One question within my survey is as follows:
Given price of X, would you purchase this: YES/NO
I need this price (X) to be randomy chosen from a bucket containing 11 prespecified numbers. 

One option was to have a hidden question at the start of the survey being an equation which generates random number from 1-11 and then using this as a key. But is there a simpler way to do this with Javascript?

2. I have 4 conditions that participants are randomly assigned to. Info at the start is different depening on condition. following are 4 question groups which are the same for all conditions. However, I need the order of the questions to be differnt. So, of all participants assigned to condition 1, half need to be have survey order 1: Q1, Q2, Q3, Q4. The other half need to have order 2: Q3, Q2, Q1, Q4. And this needs to happen for all 4 conditions. 

Any help would be greatly appreciated.

Thanks


 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #232857 by Joffm
Hi,
1. 
a. the usual way with a random number and prices that do not follow a certain arithmetic rule is - as you know
Create a random number (1-11)  {if(is_empty(rand1),rand(1,11),rand1)}
Use a nested if to display the price, like
Given price of {if(rand1==1,"€ 1.00", if(rand1==2,"€ 1.21", if(rand1==3,"€ 1.67", if(rand1==4,"€ 2.10", if(rand1==5,"€ 3.12", if(rand1==6,"€ 3.45", if(rand1==7,"€ 3.89", if(rand1==8,"€ 4.25", if(rand1==9,"€ 4.75", if(rand1==10,"€ 4.90","€ 5.00"))))))))))}, would you purchase this?

b. if the prices follow a rule you may calculate them (here the sarting price is € 1.50 and the following  always are € 0.50 higher.
Given price of {sprintf("%01.2f", rand2 * 0.5 + 1.00)}, would you purchase this?
The "sprintf" function forces two decimals.

c. Of course there is a javascript solution; but if it is simpler?
Create a question of type "short text with the following script in 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 with your prices
      var arr = ["1.00", "1.10", "1.20", "1.30", "1.40", "1.50", "1.60", "1.70", "1.80", "1.90", "2.00"];
 
// Shuffle the array
      arr = shuffle(arr);
 
// Pick only the first element of the array
      anumbers = arr.slice(0,1).join('');
// Fill the question with this element
      $('#question{QID} input[type="text"]').val(anumbers);
// After testing hide the question by removing the slashes of the next line
//    $('#question{QID}').hide();
   });
</script>
This question must not be displayed on the same page as the price question 


2. 
I am not sure if I understood the structure of your survey correctly.
You wrote

Info at the start is different depening on condition

Okay, that's easy. Random number and display of the text - either with four questions and relevance equation or by tayloring the text (depends a bit of the text)

But now you wrote

following are 4 question groups which are the same 

and

I need the order of the questions to be differnt

Which 4 question groups? Do you have a question group for each condition? Why, if they are the same?
Or are there really 4 different question groups?

Is your example of the order of questions correct?
So that Q2 and Q4 are fixed and only Q1 and Q3 aqre switched?
In this case it is sufficient to enter the same randomization group name into Q1 and Q3.
And if you want to know in which order the questions were displayed add a question of type "equation" to this group where you use the property ".qseq"
which returns the sequential number of the question
[url] www.limesurvey.org/manual/ExpressionScri...#Access_to_variables [/url]
Here you enter something like {list(Q1.qseq,Q3.qseq)}

But if you want to really have four question groups (e.g. each asking question about a different car brand) and you want to keep the same order of questions in all four groups you may do it like this
Create a random number (1-2)  (randnum)
Double the questions Q1 and Q3 and order like this and display by relevance equation.
Q1: randnum==1
Q3: randnum==2
Q2
Q1: randnum==2
Q3: randnum==1
Q4

But as I said before, I am not sure about your desired survey layout.
So the best is to send a lss export of your prototype, that shows the design.

Here a sample survey that shows this.


Best regards

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • CarmenCampagnolo
  • CarmenCampagnolo's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 3 months ago #232867 by CarmenCampagnolo
Hi Joffm,

fantastic! Everything worked. Thank you very much.

I would however have some more questions regarding the attached Iss export.

In the first question group "Condition" I have managed to use Javascript to randomly display 1 of 4 images which determines the condition (treatment group) of the participant. The issue here is that I cannot get a hidden variable displaying the condition number. I attempted to do this with question "cond11". This should be hidden for participants but I need a variable that allows me to see which condition was used.

I guess an alternative would be to use a similar code to what you mentioned before with the nested if statements, but could I do this with displaying images?

I need this image to be displayed at the start of each question group. (Questions are all related to the image). Is there a code to repeat a text display question? Cherry on top would be to have the image "frozen" at the start of each page (question group).

Can I also hide the title of each of the question groups?

Lastly, how can I insert a dollar sign in the answer field? So that subjects see answer field with dollar sign and they simply insert a number.

Again, I am very grateful for any information and help.

Thanks,
Carmen

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #232873 by Joffm
Hi,
please, if you provide a survey, provide a working one.
There were no subquestions in Q4; so you cannot activate the survey. Don't leave empty, at least enter a space.

In the first question group "Condition" I have managed to use Javascript to randomly display 1 of 4 images which determines the condition (treatment group) of the participant. The issue here is that I cannot get a hidden variable displaying the condition number.

But this is a question of type "short text" and you fill the answer field with the value. So it is stored.
But of course you do not see it in Cond11 now. The javascript is fired when the "document is ready". So Cond11 is already on the screen.
And if you put your question Cond11 into a different group you will see it.
But, what for?
Create two equations to generate the random number of the image and the price. Everything is nice and easy.
  

I guess an alternative would be to use a similar code to what you mentioned before with the nested if statements, but could I do this with displaying images?

No, as you named the images according to the random number this is sufficient, use ExpressionScript.
Code:
<img src="/lime3/upload/surveys/{SID}/files/pic/{randImg}.jpg" style="width: 792px; height: 446px;" />
Is there a reason that you did not upload to the image-folder?
  

Cherry on top would be to have the image "frozen" at the start of each page (question group).

Put it into the group description!
  

Can I also hide the title of each of the question groups?

Setting in "Presentation"
  

Lastly, how can I insert a dollar sign in the answer field? So that subjects see answer field with dollar sign and they simply insert a number.

What type of question?
You have options like "prefix" and suffix".
Or you may use an input mask.
Either
[url] igorescobar.github.io/jQuery-Mask-Plugin/ [/url]
or
[url] github.com/RobinHerbots/Inputmask [/url]


Here is a sample survey, which I think to be the best for what you showed so far.
The equation "randGrp" is still there though not necessary. But if your design changes it might be.
 

File Attachment:

File Name: limesurvey... (1).lss
File Size:40 KB

Have a look at the quesion "Order" where the order of your three groups is stored.
{if(QDC.gseq lt QOE.gseq,"Q1 Q2 Q3", "Q3 Q2 Q1")}

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • CarmenCampagnolo
  • CarmenCampagnolo's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 3 months ago #233150 by CarmenCampagnolo
Hi Joffm,

thank you very much for all the info! Super helpful once again.

Quick question, I would like to present my images using HTML. Would you have a suggestion on how to do this with the nested IF statement that you suggested previously?

Thanks,
Carmen

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #233177 by Joffm
Hi, Carmen,
it's the same
Instead of
Code:
 {if(rand1==1,"€ 1.00", if(rand1==2,"€ 1.21", ...
you enter the HTML code of the image
Code:
 {if(rand1==1,"<img src='/upload/survey/.../imageXYZ.jpg' />", if(rand1==2,"<img src='...

But beware of the many nested quotes. You can't have double quotes inside double quotes, always "double", "single", "double", "single".
The HTML Editor sometimes has its own will how to handle quotes. Better you use the source code editor.

But why not use this?
In a question of type "equation" (MyImage) you define the name of the image
Code:
{if(rand1==1,"ImageABC.jpg", if(rand1==2,"Picture XYZ.png", if(rand1==3,"TheNewImage.jpg",...

Then you use this to display the image in your question.
Code:
<img src="/lime3/upload/surveys/{SID}/files/pic/{MyImage}" style="whatever you want to style;" />

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • CarmenCampagnolo
  • CarmenCampagnolo's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 3 months ago #233217 by CarmenCampagnolo
Hi Joffm,

thanks for the quick response. I think I am however a bit confused with your answer.

You mentioned   {if(rand1==1,"<img src='/upload/survey/.../imageXYZ.jpg' />", if(rand1==2,"<img src='...

My issue is that if I use the uploaded images in my survey they get distorted if participants use phone etc.
Hence, I have created the design in Canva and have the embed code for this.

Is it possible to plugin the embed code into your suggested code? I did try inserting it in place of "<img src='/upload/survey/.../imageXYZ.jpg' />" but this dit not work.

This is an embed code for one if the images:

<div style="position: relative; width: 100%; height: 0; padding-top: 56.2500%;
 padding-bottom: 0; box-shadow: 0 2px 8px 0 rgba(63,69,81,0.16); margin-top: 1.6em; margin-bottom: 0.9em; overflow: hidden;
 border-radius: 8px; will-change: transform;">
  <iframe loading="lazy" style="position: absolute; width: 100%; height: 100%; top: 0; left: 0; border: none; padding: 0;margin: 0;"
    src="https:&#x2F;&#x2F;www.canva.com&#x2F;design&#x2F;DAFP3GIkQ-I&#x2F;view?embed" allowfullscreen="allowfullscreen" allow="fullscreen">
  </iframe>
</div>
<a href="https:&#x2F;&#x2F;www.canva.com&#x2F;design&#x2F;DAFP3GIkQ-I&#x2F;view?utm_content=DAFP3GIkQ-I&amp;utm_campaign=designshare&amp;utm_medium=embeds&amp;utm_source=link" target="_blank" rel="noopener">SOC by Carmen Campagnolo

Would you have any suggestions for this?

This is what I tried:

{if(is_empty(randImg),rand(1,4),randImg)}

{if(randImg==1,"<div style="position: relative; width: 100%; height: 0; padding-top: 56.2500%;
 padding-bottom: 0; box-shadow: 0 2px 8px 0 rgba(63,69,81,0.16); margin-top: 1.6em; margin-bottom: 0.9em; overflow: hidden;
 border-radius: 8px; will-change: transform;">
  <iframe loading="lazy" style="position: absolute; width: 100%; height: 100%; top: 0; left: 0; border: none; padding: 0;margin: 0;"
    src="https:&#x2F;&#x2F;www.canva.com&#x2F;design&#x2F;DAFP3GIkQ-I&#x2F;view?embed" allowfullscreen="allowfullscreen" allow="fullscreen">
  </iframe>
</div>
<a href="https:&#x2F;&#x2F;www.canva.com&#x2F;design&#x2F;DAFP3GIkQ-I&#x2F;view?utm_content=DAFP3GIkQ-I&amp;utm_campaign=designshare&amp;utm_medium=embeds&amp;utm_source=link" target="_blank" rel="noopener">SOC by Carmen Campagnolo", if(randImg==2,"...)}

Thank you,
Carmen 
 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #233219 by Joffm
Hi, Carmen,
without looking further, here is your first mistake
{if(randImg==1,"<div style="positio
You have nested double quotes. I mentioned that this has to be avoided.
At least it should be
{if(randImg==1,'<div style="positio
But I still do not understand why you want to repeat this whole code many times insted of only generate the variable part of the code in an equation and insert it with ExpressionScript.
I showed this at the end of my last answer.
Later I will create a very rough sample.

And to make your images responsive you should add the bootstrap class "img-responsive" as you read here
[url] getbootstrap.com/docs/3.3/css/#images [/url]


 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #233225 by Joffm
Well, in the whole code to embed this image nearly everything is constant.
Only the embed code "DAFP3GIkQ-I" depends on the image.
Now:
1. QRand (random number (1-4)  {if(is_emty(QRand),rand(1,4),QRand)}
2. QImage (nested IF to get the embed code): {If(QRand==1,"DAFP3GIkQ-I",If(QRand==2,"ABCDE-123",If(QRand==3,"XYZZXY987","123AXYPQR")))}
3. Display of the image
Code:
<div style="position: relative; width: 100%; height: 0; padding-top: 56.2500%;
 padding-bottom: 0; box-shadow: 0 2px 8px 0 rgba(63,69,81,0.16); margin-top: 1.6em; margin-bottom: 0.9em; overflow: hidden;
 border-radius: 8px; will-change: transform;">
    <iframe allow="fullscreen" allowfullscreen="allowfullscreen" loading="lazy"
        src="https://www.canva.com/design/{QImage}/view?embed" style="position: absolute; width: 100%; height: 100%; top: 0; left: 0; border: none; padding: 0;margin: 0;">
    </iframe>
</div>
 
<a href="https://www.canva.com/design/{QImage}/view?utm_content={QImage}&amp;amp;utm_campaign=designshare&amp;amp;utm_medium=embeds&amp;amp;utm_source=link" rel="noopener" target="_blank">SOC by Carmen Campagnolo</a>

You see the whoile code is constant for each image; only the embed code is piped in by ExpressionScript {QImage}

And this is what I get (okay, I have only one code)
 

 

File Attachment:

File Name: limesurvey...6957.lss
File Size:17 KB


Joffm

P.S.
Next time please provide a lss export of the relevant questions.


 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago - 2 years 3 months ago #233226 by Joffm
To add something:
I do not see the reason to refer to the image again in the link.
I expected something different.
Oh, I saw, it's automatically done by Canva

And another thing: If you download your images as *.png you avoid the whole code of Canva.
Then there is only a simple <img class="img-responsive" src="...">

And I do not see an issue on small devices
 

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 years 3 months ago by Joffm.

Please Log in to join the conversation.

  • CarmenCampagnolo
  • CarmenCampagnolo's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 2 months ago #233796 by CarmenCampagnolo
Hi Joffm,

THANK YOU so much for your help. The code worked and everything looks great!

I have one last question regarding the Order code that you recommended I use.

{if(QDC.gseq lt QOE.gseq,"Q1 Q2 Q3", "Q3 Q2 Q1")}

The QDC variable is red "Undefined variable", but when I test the survey in preview mode the questions do randomize. But I would obviously like to fully understand what I am doing here and how this is possible. Maybe you could take a look and let me know if there's anything issue you see here? Could it be due to a change in group code? Although I have adjusted the code for this.
I've attached the survey. 

I will be plugging the link of the survey in Prolific and need to integrate the ID for this.
One option is to add a question in the survey asking for the participant to insert their Prolifi ID. I have also read that one can make a hidden question that can grab the prolific ID? but I have not found any instructions for integrating Proliifc and Limesurvey. Would you have any tips for this?

Really appreciate all your help.

Thanks,
Carmen

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 2 months ago #233798 by Joffm
Hi,

QDC is a "multiple" question.
You have to enter the full QCODE of one of the subquestions, here "QDC_QDCa"

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose