Welcome to the LimeSurvey Community Forum

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

How to create a random number

More
1 year 2 weeks ago #225985 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.

More
1 year 2 weeks ago #225996 by jelo

Please Log in to join the conversation.

More
1 year 1 week ago #226044 by mohamadKabbani
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.

More
1 year 1 week ago #226046 by jelo
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.
Code:
{if(is_empty(randnumber),rand(1,4),randnumber.NAOK)}
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.

 

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.

More
1 year 1 week ago - 1 year 1 week ago #226074 by mohamadKabbani
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!!
Last edit: 1 year 1 week ago by mohamadKabbani.

Please Log in to join the conversation.

More
1 year 1 week ago #226075 by holch
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

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.

Lime-years ahead

Online-surveys for every purse and purpose