Welcome to the LimeSurvey Community Forum

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

rand() seems to behave odd

  • rakittam
  • rakittam's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #217970 by rakittam
rand() seems to behave odd was created by rakittam
Limesurvey Version: Web application, version 3.27.4

Dear Community,
I am struggling with php's random number generator, rand(). It doesn't seem to be behaving as it should.

What I am doing is, creating three allocation variables, each randomly storing either a zero or a one, based on which participants are independently assigned to a control and a treatment groups for three different question tasks. The three allocation variables are each hidden equations in the same question group, coded groupIDsocial1, groupIDsocial2 and groupIDframing, and each with the question text {rand(1,2) - 1}.

These are later used to assign participants to control and treatment groups, e.g. using "Relevance equations" ((!is_empty(groupIDsocial1.NAOK) && (groupIDsocial1.NAOK == 1)))) for treatment (or == 0 for control)). While the assignment by the relevance equations works fine, the pattern of the three random number generators is strange to me.

I activated the survey and tested the whole procedure twice and everything worked for me. My three assignment variables in the two trials were:
  1. Run: groupIDsocial1 = 0, groupIDsocial2 = 0 and groupIDframing = 1
  2. Run: groupIDsocial1 = 0, groupIDsocial2 = 1 and groupIDframing = 1
Since everything else seemed to be working well, I ran the survey with paid participants who received very strange random number patterns:
  1. Participant: groupIDsocial1 = 0, groupIDsocial2 = 0 and groupIDframing = 0
  2. Participant: groupIDsocial1 = 0, groupIDsocial2 = 0 and groupIDframing = 0
  3. Participant: groupIDsocial1 = 0, groupIDsocial2 = 0 and groupIDframing = 0
  4. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 1
  5. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 1
  6. Participant: groupIDsocial1 = 0, groupIDsocial2 = 0 and groupIDframing = 0
  7. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 1
I mean, it is possible, but unlikely, to get this pattern.... So I stopped the procedure at seven participants and made a new approach by putting each of the three allocation variables into their own question groups (with nothing in them but the respective hidden allocation equation). Then again my own two first attempts:
  1. Run: groupIDsocial1 = 0, groupIDsocial2 = 0 and groupIDframing = 1
  2. Run: groupIDsocial1 = 0, groupIDsocial2 = 1 and groupIDframing = 0
Since it seemed to be okay again, I started the experiment again with five real participants who received the following allocation values:
  1. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 1
  2. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 0
  3. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 0
  4. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 1
  5. Participant: groupIDsocial1 = 1, groupIDsocial2 = 1 and groupIDframing = 0
Is it just me or is there something wrong here? Am I wrong and everything is fine and I was just super unlucky? Or maybe someone has an idea how to get rand() to generate really randomly? I am very pleased with how well the relevance equation works in conjunction with the hidden control and treatment assignment equation for a single task (has always worked in the past), so I am very confused why I am not able to make it work for three different C/T assignments...

My next approach would be to use rand(1,2), rand(3,4) and rand(5,6) for the three hidden equations. But unfortunately, I'm running out of money and don't have many experiments left..

Let me know if I should explain more. Thank you very much for your time!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago - 2 years 9 months ago #217975 by DenisChenu
Replied by DenisChenu on topic rand() seems to behave odd
Function from PHP
github.com/LimeSurvey/LimeSurvey/blob/43...core_helper.php#L220

php.net/rand

My next approach would be to use rand(1,2), rand(3,4) and rand(5,6) for the three hidden equations. But unfortunately, I'm running out of money and don't have many experiments left..

Ask to the support for a specific survey maybe ? www.limesurvey.org/support/contact

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.
Last edit: 2 years 9 months ago by DenisChenu. Reason: https://www.limesurvey.org/support/contact
The topic has been locked.
  • rakittam
  • rakittam's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago - 2 years 9 months ago #217976 by rakittam
Replied by rakittam on topic rand() seems to behave odd
Sorry would do you mean with for a specific survey?
Last edit: 2 years 9 months ago by rakittam.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #217977 by DenisChenu
Replied by DenisChenu on topic rand() seems to behave odd

?
 
I mean : a lot of devlopper for PHP , not really a LimeSurey issue?

BUT :
use {if(is_empty(self),rand(1,2) - 1,self.NAOK)}
Else : the number is updated each time an action is done, for example when move next …
 

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.
  • rakittam
  • rakittam's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #217978 by rakittam
Replied by rakittam on topic rand() seems to behave odd
Oh, that sounds perfect, but I am not sure if I understand it right already. What is the "self" variable exactly? Do I define it or is this given by php or by limesurvey?
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #217979 by DenisChenu
Replied by DenisChenu on topic rand() seems to behave odd
self : manual.limesurvey.org/Expression_Manager....27that.27_variables

Else :
manual.limesurvey.org/Expression_Manager...e_Question_Per_Group
>  Note that the "if()" function first checks whether ask1 has already been set, and if so, uses that value. If the value hasn't been set, then it uses a random value to set the value of ask1.

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.
  • rakittam
  • rakittam's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #217999 by rakittam
Replied by rakittam on topic rand() seems to behave odd
Thank you again for your time!

So self is a reserved variable of Limesurvey. But the description in the manual is very sparse, or I don't quite understand it. How does this variable work? In my understanding self refers to the question itself which it is written in to. But what does it measure?

Trying around with the variable doesn't help. Writing self in a equation is always empty. However, writing is_empty(self) is empty too, and not 1 as I would have expected... But using a equation question with {if(is_empty(self),rand(1,2) - 1,self.NAOK)}, seems to correctly use the first if condition rand(1,2)... What is going on here?

In general, why the diversions and not just use rand(1,2) directly? What is the benefit and functioning in the diversions over the if?

Can you maybe describe specifically what happens to the self variable in an example?


Regarding manual.limesurvey.org/Expression_Manager...e_Question_Per_Group , thank you very much, is seems to work as desired! Unfortunately, I cannot understand why this strange construction works and mine won't... It seems also to use the diversions like you explained in your if-self example, maybe to somehow enforces the survey to really generate each time new random numbers with rand each time it checks the if and not sometimes reuse the rand(1,2) from a previous participant like in my example? But why would the code recompute this way and why wouldn't it when you just write three times just rand(1,2)? This seems so odd...
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #218021 by DenisChenu
Replied by DenisChenu on topic rand() seems to behave odd
self is just a shortcut for the current question code …

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.

Lime-years ahead

Online-surveys for every purse and purpose