Welcome to the LimeSurvey Community Forum

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

randomly assign participants to groups

  • jvandernoll
  • jvandernoll's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 week ago #94040 by jvandernoll
randomly assign participants to groups was created by jvandernoll
Hi,
I try to create something that will assign participants randomly to one of 4 groups, but I don't get it to work.

I work with LS 1.91, so I cannot add "question type equation". I have found some solutions, but I run into problems, so I think I make a mistake somewhere.

The following script I adjusted from another forum post (which can be found here www.limesurvey.org/en/forum/can-i-do-thi...-the-others?start=10 )

<script type="text/javascript">
<!--
$(document).ready(function()
{
// Find a random number between 1 and 4
var randNumber = Math.floor(Math.random()*3 + 1);
$('input#answer96682X129523X1961293').val(randNumber);
checkconditions(randNumber,'96682X129523X1961293','text');
})
//-->
</script>

where 96682 is Survey ID, 129523 is Group ID, 1961293 is Question ID)

In the next question group I created 4 questions, and depending on the random "answer" participants would get one of the 4 questions (by adding the condition "answer to 'random' question equals 1" --> question 1; "...equals 2" --> question 2, etc.). This seems to work.

When I try to hide this question, however, it does no longer work (the question is hidden, but the questions following this question are not shown either).

Also, if I have 4 groups, is the "Math.floor(Math.random()*3 + 1)" correct, or should it be "Math.floor(Math.random()*4 + 1)" (as is shown in the link below). And are the random numbers obtained than 1 2 3 and 4 or 0 1 2 and 3? I have tested the above solution a number of times, but only got 1 2 or 3 as a response. Not sure if that is just coincidence, or that I actually only create those 3 numbers.

This is another solution that I found, but it did not work for me:
docs.limesurvey.org/tiki-index.php?page=...rol_survey_behaviour

I hope someone can help!
Many thanks in advance!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 week ago #94058 by DenisChenu
Replied by DenisChenu on topic randomly assign participants to groups
Hello,

With 2.0 (or 1.92), you can use directly:
{rand(1,4)} in an equation question type.

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • jvandernoll
  • jvandernoll's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 week ago #94060 by jvandernoll
Replied by jvandernoll on topic randomly assign participants to groups
Thanks Denis,
Unfortunately, I work (via university server) with version 1.91, and don't have that option.
Any thoughts on how to solve this in 1.91?

Jolanda
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 week ago #94079 by tpartner
Replied by tpartner on topic randomly assign participants to groups

When I try to hide this question, however, it does no longer work...

You cannot hide the question with the advanced question setting, You need to hide it with JavaScript:
Code:
$('#question96682X129523X1961293').hide();
or with CSS
Code:
#question96682X129523X1961293 { display: none; }

Also, if I have 4 groups, is the "Math.floor(Math.random()*3 + 1)" correct, or should it be "Math.floor(Math.random()*4 + 1)"...

To get a random number between 1 and 4, use
Code:
Math.floor(Math.random()*4)+1

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • jvandernoll
  • jvandernoll's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 week ago - 11 years 1 week ago #94081 by jvandernoll
Replied by jvandernoll on topic randomly assign participants to groups
Thanks Tony!
I included the code for hiding the question in my javascript source, but it keeps showing the question...? Do you see the mistake in the code?

The source code that I have now is:
Code:
<script type="text/javascript">
<!--
$(document).ready(function()
{
// Find a random number between 1 and 4
var randNumber = Math.floor(Math.random()*4 + 1);
$('input#answer96682X129523X1961293').val(randNumber);
checkconditions(randNumber,'96682X129523X1961293','text');
$('#question96682X129523X1961293').hide();
})
//-->
</script>

Thanks!
Jolanda.
Last edit: 11 years 1 week ago by DenisChenu. Reason: [code type=javascript]
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 week ago #94086 by DenisChenu
Replied by DenisChenu on topic randomly assign participants to groups
Thinks it's $('#question96682').hide(); :)

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • jvandernoll
  • jvandernoll's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 week ago #94088 by jvandernoll
Replied by jvandernoll on topic randomly assign participants to groups
Yes! That works, great!!
It still shows the page of the question, but if I add this question to another group, people won't notice.

Thanks!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 week ago #94089 by tpartner
Replied by tpartner on topic randomly assign participants to groups
Oops, sorry, moving too fast...

I think it should be :
Code:
$('#question1961293').hide();

Or CSS:
Code:
#question1961293 { display: none; }


(Assuming the question ID is 1961293)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DenisChenu, jvandernoll
The topic has been locked.
  • jvandernoll
  • jvandernoll's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #100605 by jvandernoll
Replied by jvandernoll on topic randomly assign participants to groups
Hi again,
following my earlier question - Now try to do the same with three conditional groups. I adjusted the code I used before (see below) to this:

<script type="text/javascript">
<!--
$(document).ready(function()
{
// Find a random number between 1 and 3
var randNumber = Math.floor(Math.random()*3 + 1);
$('input#answer96682X129523X1961293').val(randNumber);
checkconditions(randNumber,'96682X129523X1961293','text');
$('#question96682X129523X1961293').hide();
})
//-->
</script>

but this does not work. Can you spot the mistake?

Many thanks!
The topic has been locked.
More
10 years 4 months ago #101344 by cecilia
Replied by cecilia on topic randomly assign participants to groups
Please, could you send me a copy of a survey applying the discussed function? I have access to LS 1.91+ from my University. I have changed the js template but I cannot use the question with the random number in conditional format. Thanks!
The topic has been locked.
  • jvandernoll
  • jvandernoll's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago #101488 by jvandernoll
Replied by jvandernoll on topic randomly assign participants to groups
Hi Cecilia,
If you go to your question, you should include the code below in your source code (click source)
- This is for 4 conditions;
- you have to adjust the SurveyIdXGroupIdXQuestionId to correspond with your own Id-numbers (leave the X between the Id-numbers);
- and the last line $('#question1979503').hide(); is to hide the question with question Id number 1979503 -- you need to adjust this too to correspond with your question ID

Hope this helps!

Here's the code:


<script type="text/javascript">
<!--
$(document).ready(function()
{
// Find a random number between 1 and 4
var randNumber = Math.floor(Math.random()*4 + 1);
$('input#answerSurveyIdXGroupIdXQuestionId').val(randNumber);
checkconditions(randNumber,'SurveyIdXGroupIdXQuestionId','text');
$('#question1979503').hide();
})
//-->
</script>
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose