- Posts: 10
- 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
2 years 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.
- jelo
- Offline
- Platinum Member
Less
More
- Posts: 5070
- Thank you received: 1263
2 years 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: 10
- Thank you received: 0
2 years 2 weeks 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.
- jelo
- Offline
- Platinum Member
Less
More
- Posts: 5070
- Thank you received: 1263
2 years 2 weeks 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: 10
- Thank you received: 0
2 years 2 weeks ago - 2 years 2 weeks 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: 2 years 2 weeks ago by mohamadKabbani.
Please Log in to join the conversation.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11758
- Thank you received: 2753
2 years 2 weeks 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.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12790
- Thank you received: 3940
2 years 2 weeks ago - 2 years 2 weeks ago #226076
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic How to create a random number
Well, that's easy.
Create a random number between 1 and 99999 and divide by 100000.
{if(is_empty(randnumber),rand(1,99999)/100000,randnumber.NAOK)}
Or between 1 and 99 and divide by 100.
Depends on the number of decimals you would like to have.
Joffm
Create a random number between 1 and 99999 and divide by 100000.
{if(is_empty(randnumber),rand(1,99999)/100000,randnumber.NAOK)}
Or between 1 and 99 and divide by 100.
Depends on the number of decimals you would like to have.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 years 2 weeks ago by Joffm.
Please Log in to join the conversation.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11758
- Thank you received: 2753
2 years 2 weeks ago #226078
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
This will work for numbers that are not 0. To include 1 (as the request was), one can go from 1 to 100 and devide by 100. But what about zero?
I don't know how important it is to actually include zero, but...
I don't know how important it is to actually include zero, but...
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.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12790
- Thank you received: 3940
2 years 2 weeks ago #226083
by Joffm
But the idea was clear, how to create decimals.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic How to create a random number
Oh, Oh, My glasses get worse and worse.To include 1 (as the request was)
But the idea was clear, how to create decimals.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Please Log in to join the conversation.