- Posts: 9
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
How to create a random number
- mohamadKabbani
-
Topic Author
- Offline
- New Member
-
Less
More
1 year 2 weeks ago #225985
by mohamadKabbani
How to create a random number was created by mohamadKabbani
I need to know how to generate a stand alone random number in a survey and use that number in an equation.
Please Log in to join the conversation.
1 year 2 weeks ago #225996
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Replied by jelo on topic How to create a random number
You can learn that from the examples.
manual.limesurvey.org/Expression_Manager...e_Question_Per_Group
manual.limesurvey.org/Expression_Manager...e_Question_Per_Group
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Please Log in to join the conversation.
- mohamadKabbani
-
Topic Author
- Offline
- New Member
-
Less
More
- Posts: 9
- Thank you received: 0
1 year 1 week ago #226044
by mohamadKabbani
Replied by mohamadKabbani on topic How to create a random number
I don't want to randomize my questions, I want to create a random number to be multiplied in an hidden equation . for example (arrival time * cost / random number between 1 and 4) how can I do that ??
Please Log in to join the conversation.
1 year 1 week ago #226046
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Replied by jelo on topic How to create a random number
As I said you can learn that from these examples. You can see the usage of rand in the example.
rand
Generate a random integer, see
this example
int rand() OR int rand(min, max)
Short Example:
You can create a equation question with the name "randnumber" and place this ExpressionScript inside the question.
That creates a stable randomnumber between 1 and 4
With stable I mean the checking for an value via if clause. Rand is triggered every time a survey respondent is reaching the random question. That behaviour is mostly not wanted cause the number would changes during a survey visit. I recommend to create randomnumber at the beginning of the survey and use the variables inside other equations as you like.
Short Example:
You can create a equation question with the name "randnumber" and place this ExpressionScript inside the question.
Code:
{if(is_empty(randnumber),rand(1,4),randnumber.NAOK)}
With stable I mean the checking for an value via if clause. Rand is triggered every time a survey respondent is reaching the random question. That behaviour is mostly not wanted cause the number would changes during a survey visit. I recommend to create randomnumber at the beginning of the survey and use the variables inside other equations as you like.
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Please Log in to join the conversation.
- mohamadKabbani
-
Topic Author
- Offline
- New Member
-
Less
More
- Posts: 9
- Thank you received: 0
1 year 1 week ago - 1 year 1 week ago #226074
by mohamadKabbani
Replied by mohamadKabbani on topic How to create a random number
Thank you this code is so helpful, one last question
if I want to use the same code but to generate rand number between 0 & 1
with decimals example "0.43223" how would the could change I tired making it "{if(is_empty(randnumber),rand(),randnumber.NAOK)}" but it did not work thank you!!
if I want to use the same code but to generate rand number between 0 & 1
with decimals example "0.43223" how would the could change I tired making it "{if(is_empty(randnumber),rand(),randnumber.NAOK)}" but it did not work thank you!!
Last edit: 1 year 1 week ago by mohamadKabbani.
Please Log in to join the conversation.
1 year 1 week ago #226075
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic How to create a random number
The rand() function only generates integers.
www.php.net/manual/en/function.rand.php
You will probably have to do something like this:
stackoverflow.com/questions/10419501/use...eteween-two-decimals
www.php.net/manual/en/function.rand.php
You will probably have to do something like this:
stackoverflow.com/questions/10419501/use...eteween-two-decimals
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Please Log in to join the conversation.