Welcome to the LimeSurvey Community Forum

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

How to create a random number

  • mohamadKabbani
  • mohamadKabbani's Avatar Topic Author
  • Offline
  • New Member
  • New Member
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.

More
2 years 2 weeks ago #225996 by jelo
Replied by jelo on topic How to create a random number

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
  • mohamadKabbani's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
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.

More
2 years 2 weeks 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.

  • mohamadKabbani
  • mohamadKabbani's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
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!!
Last edit: 2 years 2 weeks ago by mohamadKabbani.

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 2 weeks 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.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 2 weeks ago - 2 years 2 weeks ago #226076 by Joffm
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
 

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
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 2 weeks ago #226078 by holch
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 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
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 2 weeks ago #226083 by Joffm
Replied by Joffm on topic How to create a random number

To include 1 (as the request was)

Oh, Oh, My glasses get worse and worse.

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.

Lime-years ahead

Online-surveys for every purse and purpose