Welcome to the LimeSurvey Community Forum

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

expression manager and random answer

  • marcomarenco
  • marcomarenco's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 week ago - 7 years 1 week ago #150148 by marcomarenco
expression manager and random answer was created by marcomarenco
Hi,
i've created an hidden question ( type is multiple choice), i would use a random number to get one of the subanswers and to print this in another question (boilerplate question).

it is possible to use something like this:

{A0FB=FBPNOT_SQ + rand(1,43) + .question}

A0FB is a boilerplate question

FBPNOT is the multiple question, with 43 subanswers.

_SQ1 to _SQ43 are the subanswers.

Or is it possible to set a variable, e.g. number=rand(1,43) and after use "if"


:) thanks in advance.

Marco
Last edit: 7 years 1 week ago by marcomarenco.
The topic has been locked.
More
7 years 1 week ago #150151 by jelo
Replied by jelo on topic expression manager and random answer

marcomarenco wrote: i would use a random number to get one of the subanswer to print this subanswer in another question.

{A0FB=FBPNOT_SQ + rand(1,43) + .question}


You cannot take a value of a variable and use it create another variable NAME.
FBPNOT_SQ+rand(1,43)+.question will not be seen as one variable.


In other tools you would even not need to use a hidden question.
You would just create a static list and choose one item via one command randomly.
See feature request for dynamic / static list here:
bugs.limesurvey.org/view.php?id=11688


Workaround
You want one answer randomly assigned to a variable, which you can display somewhere in another question.
First I would create a separate equation question which just contains rand(1,43) to save the random number.

Approach 1:
Nested ifs with 43 answers depending on random number
manual.limesurvey.org/Expression_Manager...Conditional_Logic.29

Approach 2:
Assign a default answer via random number
manual.limesurvey.org/Expression_Manager..._Assignment_Operator

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The following user(s) said Thank You: marcomarenco
The topic has been locked.
  • marcomarenco
  • marcomarenco's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 week ago #150184 by marcomarenco
Replied by marcomarenco on topic expression manager and random answer
solved in this way:
{A0FB=if(is_empty(A0.NAOK), if((rand(1,43)==1),FBPNOT_SQ1.question,if((rand(1,43)==3),FBPNOT_SQ3.question,if((rand(1,43)==4),FBPNOT_SQ4.question,if((rand(1,43)==5),FBPNOT_SQ5.question,if((rand(1,43)==6),FBPNOT_SQ6.question,if((rand(1,43)==7),FBPNOT_SQ7.question,if((rand(1,43)==8),FBPNOT_SQ8.question,if((rand(1,43)==9),FBPNOT_SQ9.question,if((rand(1,43)==10),FBPNOT_SQ10.question,if((rand(1,43)==11),FBPNOT_SQ11.question,if((rand(1,43)==12),FBPNOT_SQ12.question,if((rand(1,43)==13),FBPNOT_SQ13.question,if((rand(1,43)==14),FBPNOT_SQ14.question,if((rand(1,43)==15),FBPNOT_SQ15.question,if((rand(1,43)==16),FBPNOT_SQ16.question,if((rand(1,43)==17),FBPNOT_SQ17.question,if((rand(1,43)==18),FBPNOT_SQ18.question,if((rand(1,43)==19),FBPNOT_SQ19.question,if((rand(1,43)==20),FBPNOT_SQ20.question,if((rand(1,43)==21),FBPNOT_SQ21.question,if((rand(1,43)==22),FBPNOT_SQ22.question,if((rand(1,43)==23),FBPNOT_SQ23.question,if((rand(1,43)==24),FBPNOT_SQ24.question,if((rand(1,43)==25),FBPNOT_SQ25.question,if((rand(1,43)==26),FBPNOT_SQ26.question,if((rand(1,43)==27),FBPNOT_SQ27.question,if((rand(1,43)==28),FBPNOT_SQ28.question,if((rand(1,43)==29),FBPNOT_SQ29.question,if((rand(1,43)==30),FBPNOT_SQ30.question,if((rand(1,43)==31),FBPNOT_SQ31.question,if((rand(1,43)==32),FBPNOT_SQ32.question,if((rand(1,43)==33),FBPNOT_SQ33.question,if((rand(1,43)==34)FBPNOT_SQ34.question,if((rand(1,43)==35),FBPNOT_SQ35.question,if((rand(1,43)==36),FBPNOT_SQ36.question,if((rand(1,43)==37),FBPNOT_SQ37.question,if((rand(1,43)==38),FBPNOT_SQ38.question,if((rand(1,43)==39),FBPNOT_SQ39.question,if((rand(1,40)==2),FBPNOT_SQ40.question,if((rand(1,43)==41),FBPNOT_SQ41.question,if((rand(1,43)==42),FBPNOT_SQ42.question,if((rand(1,43)==43),FBPNOT_SQ43.question,if((rand(1,43)==2),FBPNOT_SQ2.question,FBPNOT_SQ2.question))))))))))))))))))))))))))))))))))))))))))), if((rand(1,43)==1),FBPOK_SQ1.question,if((rand(1,43)==3),FBPOK_SQ3.question,if((rand(1,43)==4),FBPOK_SQ4.question,if((rand(1,43)==5),FBPOK_SQ5.question,if((rand(1,43)==6),FBPOK_SQ6.question,if((rand(1,43)==7),FBPOK_SQ7.question,if((rand(1,43)==8),FBPOK_SQ8.question,if((rand(1,43)==9),FBPOK_SQ9.question,if((rand(1,43)==10),FBPOK_SQ10.question,if((rand(1,43)==11),FBPOK_SQ11.question,if((rand(1,43)==12),FBPOK_SQ12.question,if((rand(1,43)==13),FBPOK_SQ13.question,if((rand(1,43)==14),FBPOK_SQ14.question,if((rand(1,43)==15),FBPOK_SQ15.question,if((rand(1,43)==16),FBPOK_SQ16.question,if((rand(1,43)==17),FBPOK_SQ17.question,if((rand(1,43)==18),FBPOK_SQ18.question,if((rand(1,43)==19),FBPOK_SQ19.question,if((rand(1,43)==20),FBPOK_SQ20.question,if((rand(1,43)==21),FBPOK_SQ21.question,if((rand(1,43)==22),FBPOK_SQ22.question,if((rand(1,43)==23),FBPOK_SQ23.question,if((rand(1,43)==24),FBPOK_SQ24.question,if((rand(1,43)==25),FBPOK_SQ25.question,if((rand(1,43)==26),FBPOK_SQ26.question,if((rand(1,43)==27),FBPOK_SQ27.question,if((rand(1,43)==28),FBPOK_SQ28.question,if((rand(1,43)==29),FBPOK_SQ29.question,if((rand(1,43)==30),FBPOK_SQ30.question,if((rand(1,43)==31),FBPOK_SQ31.question,if((rand(1,43)==32),FBPOK_SQ32.question,if((rand(1,43)==33),FBPOK_SQ33.question,if((rand(1,43)==34)FBPOK_SQ34.question,if((rand(1,43)==35),FBPOK_SQ35.question,if((rand(1,43)==36),FBPOK_SQ36.question,if((rand(1,43)==37),FBPOK_SQ37.question,if((rand(1,43)==38),FBPOK_SQ38.question,if((rand(1,43)==39),FBPOK_SQ39.question,if((rand(1,40)==2),FBPOK_SQ40.question,if((rand(1,43)==41),FBPOK_SQ41.question,if((rand(1,43)==42),FBPOK_SQ42.question,if((rand(1,43)==43),FBPOK_SQ43.question,if((rand(1,43)==2),FBPOK_SQ2.question,FBPOK_SQ2.question))))))))))))))))))))))))))))))))))))))))))))}
The topic has been locked.
More
7 years 1 week ago #150187 by jelo
Replied by jelo on topic expression manager and random answer
Well, I would be surprised if you get a even distributed exposure of questions.

You let choose the random number 43 times in the equation.
Since the nested ifs are evaluated in a sequential way, you most likely will end up with a lot of chosen questions in the higher range. E.g. rand=1 could only be choosen, if the 42 other rand(1,43) are no matching in the next 42 times. That is very uncommon.

To ensure a evenly distribution of random number, you should create a equation question and use rand(1,43) one time. This equation question code should replace the rand(1,43) in your nested if.
The performance be will better too, since a random number is calculated one time only.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The following user(s) said Thank You: holch, tpartner
The topic has been locked.
  • marcomarenco
  • marcomarenco's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 week ago #150190 by marcomarenco
Replied by marcomarenco on topic expression manager and random answer
yes, everytime i create a random number, maybe it needs a little time more, and it needs more resource (not so much). in this way i try to simulate a number create randomly. the last "if" is fixed, so something appear on the screen, maybe to be more sure that i can match the random number with a if statement, i've to duplicate the same condition 2 or 3 times.

i'm trying someother way to solve this :)
bye
The topic has been locked.
More
7 years 1 week ago #150194 by jelo
Replied by jelo on topic expression manager and random answer

marcomarenco wrote: in this way i try to simulate a number create randomly.

Your approach is having a bias.
You're not getting a more random number if you generate a new random number with every if.
You get the opposite, since the order of the nested if is always from 1 to 43.

I was wrong about the higher range, since "nested if" stops if one "if" is true.
The order of "nested if" determinate the chances of getting chosen.

You have FBPNOT_SQ43.question,if((rand(1,43)==2),FBPNOT_SQ2.question,FBPNOT_SQ2.question at the end of the first "nested if".

To get a "if((rand(1,43)==2),FBPNOT_SQ2.question" the 41 ifs before have to be FALSE in addition.
To get a "if((rand(1,43)==1),FBPNOT_SQ1.question" only this needs to be true.
So the chances of getting question2 is a lot lower that getting question1.
Not sure why you put question2 at the end of the nested if. But good for testing.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • marcomarenco
  • marcomarenco's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 week ago #150196 by marcomarenco
Replied by marcomarenco on topic expression manager and random answer
maybe i've found another way:
1. create an hidden equation question (the first) named "random".
2. in another question (boilerplate) es. A0FB, in relevance i've put: random=rand(1, 43)

now i can check the value of random to print a random feedback.

what do you think?

Marco
The topic has been locked.
More
7 years 1 week ago #150201 by jelo
Replied by jelo on topic expression manager and random answer

marcomarenco wrote: maybe i've found another way:
1. create an hidden equation question (the first) named "random".

You've completely lost me. Last attempt with a clean sheet.

You want to display a question-text (randomly chosen from 43 questions texts) in a boilerplate question.

Simplest way:

1. Question:
Type: Equation question
Name: randnumber
Questiontext
if( ! is_empty(randnumber), randnumber, rand(1,43))

2. Question:
Type: Boilerplate

Questiontext:
{if randnumber=1,"Questiontext1-...sdsdjkadljsdlsjdsad",""}
{if randnumber=2,"Questiontext2-...sdsdjkadljsdlsjdsad",""}
etc..

To access FBPNOT_SQ1.question etc. you can use
{if randnumber=1,FBPNOT_SQ1.question,""}
{if randnumber=2,FBPNOT_SQ2.question,""}


If you want something completely different you might explain your task again and attach a surveyfile to this thread.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • marcomarenco
  • marcomarenco's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 week ago #150210 by marcomarenco
Replied by marcomarenco on topic expression manager and random answer
i try to explain, it's a survey with anagrams to solve, i use short text and regular expression to check if the person find the right solution. there are 30 anagrams, and for every anagrams there's a verbal feedback (boilerpalte question) and i would that this fb are choosen randomly from list (now there are 43 feedback if the person find the solution, 43 if he don't find the solution).
i need that this feedback are different everytime, this is the reason of this post.
in my first attempt i follow this example:
survey-consulting.com/how-to-randomly-sh...a-limesurvey-survey/

and it's similar to your solution, i've tried to use, but the randnumber question was always the same, i need that the random number change for every feedback, i'm trying now and if i put in relevance equation:
randnumber=rand(1,43)
it changes.

thanks for your help :)
Marco
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose