- Posts: 6
- Thank you received: 0
using rand() and mandatory simultaneously
5 years 2 months ago - 5 years 2 months ago #91940
by Anaben11
Anaben11 created the topic: using rand() and mandatory simultaneously
Hello,
my survey is supposed to show one randomly chosen question from a pool of 31 questions. This random question needs to be set on "mandatory".
The problem is that, if you answer the random question and click on the next-button to get forwarded to the next question group, the next question from the pool of now 30 remaining questions is asked. This continues until all questions are asked, instead of just one.
In the random question I use rand(1,31), the relevance equations for the other questions are set to randnumber.NAOK==x.
I asked this question already in the bug report area of this site, because I thought it was one, but I got one answer there: I was told to set the equation type question (thus the one with rand()) so that numbers are returned only ... well I did that, but it still just doesn't work, I have no idea how figure it out.
I use Version 2.00+ Build 130129
I attached the LSS file, if somebody wants to take a look at it.
I'd be very greatful for your help, I need to set up this survey for a thesis, it's really important to me
Thank you in advance!!
my survey is supposed to show one randomly chosen question from a pool of 31 questions. This random question needs to be set on "mandatory".
The problem is that, if you answer the random question and click on the next-button to get forwarded to the next question group, the next question from the pool of now 30 remaining questions is asked. This continues until all questions are asked, instead of just one.
In the random question I use rand(1,31), the relevance equations for the other questions are set to randnumber.NAOK==x.
I asked this question already in the bug report area of this site, because I thought it was one, but I got one answer there: I was told to set the equation type question (thus the one with rand()) so that numbers are returned only ... well I did that, but it still just doesn't work, I have no idea how figure it out.
I use Version 2.00+ Build 130129
I attached the LSS file, if somebody wants to take a look at it.
I'd be very greatful for your help, I need to set up this survey for a thesis, it's really important to me

Thank you in advance!!
Last Edit: 5 years 2 months ago by Anaben11.
Please Log in or Create an account to join the conversation.
- DenisChenu
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 8889
- Karma: 400
- Thank you received: 1469
5 years 2 months ago #91952
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
DenisChenu replied the topic: using rand() and mandatory simultaneously
Hello,
Best is to use 2 group.
In the first:
Equation: rand(1,31)
In the second, you take the result of the equation to set the condition. If your user can not come back (option in survey), then no change.
Best is to use 2 group.
In the first:
Equation: rand(1,31)
In the second, you take the result of the equation to set the condition. If your user can not come back (option in survey), then no change.
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Please Log in or Create an account to join the conversation.
5 years 2 months ago #91959
by Anaben11
Anaben11 replied the topic: using rand() and mandatory simultaneously
hey,
thanks for your reply. What do you mean exactly by taking the result of the equation to set the condition? How would you set up the second group?
thanks for your reply. What do you mean exactly by taking the result of the equation to set the condition? How would you set up the second group?
Please Log in or Create an account to join the conversation.
Less
More
- Posts: 6743
- Karma: 601
- Thank you received: 1811
5 years 2 months ago - 5 years 2 months ago #91972
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
tpartner replied the topic: using rand() and mandatory simultaneously
If, for example, the question code for the Equation type question is "randNumber", then the relevance equations for the questions in group 2 would be:......
If a question is hidden by relevance the mandatory requirement is overruled.
randNumber.shown == 1
randNumber.shown == 2
randNumber.shown == 3
randNumber.shown == 4
If a question is hidden by relevance the mandatory requirement is overruled.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 2 months ago by tpartner.
Please Log in or Create an account to join the conversation.
- DenisChenu
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 8889
- Karma: 400
- Thank you received: 1469
5 years 2 months ago #91976
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
DenisChenu replied the topic: using rand() and mandatory simultaneously
Yes: put the rand number in another group.
But here there are clearly a bug.
I reopen the bug tracker.
Alternative: with the same page, if your random questio code are RAND:
Put this in equation:
{if(RAND.NAOK, RAND.NAOK, rand(1, 9))}
But here there are clearly a bug.
I reopen the bug tracker.
Alternative: with the same page, if your random questio code are RAND:
Put this in equation:
{if(RAND.NAOK, RAND.NAOK, rand(1, 9))}
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Please Log in or Create an account to join the conversation.
5 years 2 months ago #91977
by TMSWhite
If you depend upon LimeSurvey or the support team, please give generously to our 2012 fundraiser .
TMSWhite replied the topic: using rand() and mandatory simultaneously
This may not be a bug. Take a look at this
older thread
and this
sample survey
. In both cases, you have to check whether the random number has been assigned before calling rand() in order to prevent rand() from being called multiple times. That is actually the desired behavior of Expression Manager, which validates everything on both the client and server sides to avoid loss of data integrity.
If you depend upon LimeSurvey or the support team, please give generously to our 2012 fundraiser .
- Expression Manager (EM):
Documentation
|
Sample Surveys
|
How Tos
|
Show Logic File
|
Upgrading to 1.92+ - Excel Style for Survey Structure Import/Export
Please Log in or Create an account to join the conversation.
- DenisChenu
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 8889
- Karma: 400
- Thank you received: 1469
5 years 2 months ago #91979
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
DenisChenu replied the topic: using rand() and mandatory simultaneously
Maybe,
But if you do:
1st equation question: RANDOM : {rand(1,100)}
2nd equation question: RETURN : {RANDOM}
You have different number at survey taking. Don't think it had to happen. After click on Next : OK, why not but not at page view.
Denis
But if you do:
1st equation question: RANDOM : {rand(1,100)}
2nd equation question: RETURN : {RANDOM}
You have different number at survey taking. Don't think it had to happen. After click on Next : OK, why not but not at page view.
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 (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Please Log in or Create an account to join the conversation.
5 years 2 months ago #91982
by mdekker
---
Menno Dekker
mdekker replied the topic: using rand() and mandatory simultaneously
The reference to {RANDOM} causes the expression to be evaluated. The expression should make sure it only returns a random value when it was not already there so that even on reload of the page it stays the same.
From EM perspective this is completely correct. From the user point of view, it can be difficult. Maybe with the coming question plugins we can create a random number question that does exactly what most users want, only generate the number once and store it in db. For now I think the random needs to be documented in the wiki. There already is a sample survey, and maybe from the function list a pointer to the sample survey should be added. I will add that.
From EM perspective this is completely correct. From the user point of view, it can be difficult. Maybe with the coming question plugins we can create a random number question that does exactly what most users want, only generate the number once and store it in db. For now I think the random needs to be documented in the wiki. There already is a sample survey, and maybe from the function list a pointer to the sample survey should be added. I will add that.
---
Menno Dekker
Please Log in or Create an account to join the conversation.
5 years 2 months ago #91984
by Anaben11
Anaben11 replied the topic: using rand() and mandatory simultaneously
thanks so much guys!!
I implemented the solution with two question groups and it seems to work perfectly fine!!
I implemented the solution with two question groups and it seems to work perfectly fine!!
Please Log in or Create an account to join the conversation.
- DenisChenu
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 8889
- Karma: 400
- Thank you received: 1469
5 years 2 months ago #91985
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
DenisChenu replied the topic: using rand() and mandatory simultaneously
I completely understand at reload page OR at submit page.
But why at page view ?
Denis
But why at page view ?
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 (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Please Log in or Create an account to join the conversation.
5 years 2 months ago #91994
by TMSWhite
If you depend upon LimeSurvey or the support team, please give generously to our 2012 fundraiser .
TMSWhite replied the topic: using rand() and mandatory simultaneously
Denis-
The EM processing flow works as follows:
(1) [Server-side] Find next set of relevant questions. So, processes the relevance equations of each group to find the next group that has at least one relevant, non-hidden question. Thus, rand() is called there
(2) [Client-side] It should not need to call the relevance equations again prior to the first page view, since the generated HTML will have the proper visibility based upon the server-side calculated relevance. This was certainly true in 1.92. There was a time when 2.0 called the EM JavaScript functions onload(), which would call rand() again. This isn't needed, but may still be happening (I haven't checked and don't have an easy way to do so).
(3) [Client-side] With each navigation change, the validation equations are processed, so rand() could be called multiple times on the page
(4) [Client-side] When Submit is pressed, there is no additional client-side validation performed, since the validation would have just occurred between the onblur event from the prior data entry field and the onfocus event arriving at the submit button
(5) [Server-side] LimeSurvey re-validates all submitted data, even though it was also validated client-side. This ensures data integrity and prevents spoofing of results.
So, as you can see, validation can be called multiple times, which is why you need the following approach for rand() (where I am assuming that you have an Equation question type called myRand and you want random values between x and y
Denis, the one thing worth checking is whether the client-side call mentioned in (2) is still happening. If so, it can be removed, which might address your concern. There is an open bug ticket about that - bugs.limesurvey.org/view.php?id=5776
The EM processing flow works as follows:
(1) [Server-side] Find next set of relevant questions. So, processes the relevance equations of each group to find the next group that has at least one relevant, non-hidden question. Thus, rand() is called there
(2) [Client-side] It should not need to call the relevance equations again prior to the first page view, since the generated HTML will have the proper visibility based upon the server-side calculated relevance. This was certainly true in 1.92. There was a time when 2.0 called the EM JavaScript functions onload(), which would call rand() again. This isn't needed, but may still be happening (I haven't checked and don't have an easy way to do so).
(3) [Client-side] With each navigation change, the validation equations are processed, so rand() could be called multiple times on the page
(4) [Client-side] When Submit is pressed, there is no additional client-side validation performed, since the validation would have just occurred between the onblur event from the prior data entry field and the onfocus event arriving at the submit button
(5) [Server-side] LimeSurvey re-validates all submitted data, even though it was also validated client-side. This ensures data integrity and prevents spoofing of results.
So, as you can see, validation can be called multiple times, which is why you need the following approach for rand() (where I am assuming that you have an Equation question type called myRand and you want random values between x and y
{if(is_empty(myRand),rand(x,y),myRand)}
Denis, the one thing worth checking is whether the client-side call mentioned in (2) is still happening. If so, it can be removed, which might address your concern. There is an open bug ticket about that - bugs.limesurvey.org/view.php?id=5776
If you depend upon LimeSurvey or the support team, please give generously to our 2012 fundraiser .
- Expression Manager (EM):
Documentation
|
Sample Surveys
|
How Tos
|
Show Logic File
|
Upgrading to 1.92+ - Excel Style for Survey Structure Import/Export
Please Log in or Create an account to join the conversation.
- DenisChenu
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 8889
- Karma: 400
- Thank you received: 1469
5 years 2 months ago - 5 years 2 months ago #92016
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
DenisChenu replied the topic: using rand() and mandatory simultaneously
Hello Thomas,
Maybe you're right, 2 still happening, maybe javascript don't have to update rand ?
Question:
If in RDP_ValidFunctions we set rand to:(removed rand in javascript. It will do the job ?
Oh no, js have to update rand.
Because of rand(1,QCODE) with QCODE in the same page ....
Denis
Maybe you're right, 2 still happening, maybe javascript don't have to update rand ?
Question:
If in RDP_ValidFunctions we set rand to:
'rand' => array('rand', '', $this->gT('Generate a random integer'), 'int rand() OR int rand(min, max)', 'http://www.php.net/manual/en/function.rand.php', 0,2),
Oh no, js have to update rand.
Because of rand(1,QCODE) with QCODE in the same page ....
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 (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Last Edit: 5 years 2 months ago by DenisChenu. Reason: typo: ith -> with
Please Log in or Create an account to join the conversation.