Random selection of questions with varying question lists

I want to randomly select one crop from a list to ask questions about. I
know it's possible to use the RANDOM function and set display conditions
based on that number when the list of options if fixed, but what about when
the crops that farmers plant vary from case to case? For example, farmers
may be growing any combination of maize, potatoes, and two types of beans
(so 1-4 crops). Based on the crops that they’re growing, we want to
randomly select one crop to ask questions about.

Thanks!

Eric

Eric,

You should be able to do this using the same pattern as you would for
displaying a random question from a fixed set. The only change would be
that instead of generating random with fixed inputs you'd use a variable to
calculate counting the number of questions as the input. For example if
your user was selecting their crop from a select list you could use the
count-selected
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-count-selected
function
to determine this.

Cheers,
Will

··· On Mon, Oct 2, 2017 at 6:13 AM, eric.jospe via commcare-users < commcare-users@googlegroups.com> wrote:

I want to randomly select one crop from a list to ask questions about. I
know it's possible to use the RANDOM function and set display conditions
based on that number when the list of options if fixed, but what about when
the crops that farmers plant vary from case to case? For example, farmers
may be growing any combination of maize, potatoes, and two types of beans
(so 1-4 crops). Based on the crops that they’re growing, we want to
randomly select one crop to ask questions about.

Thanks!

Eric

--
You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hey Will,

Could you please be more specific? What is the actual syntax I should use
on the hidden value and the display conditions for the questions?

Thanks,

Eric

··· On Monday, October 2, 2017 at 12:24:57 PM UTC+2, William Pride wrote: > > Eric, > > You should be able to do this using the same pattern as you would for > displaying a random question from a fixed set. The only change would be > that instead of generating random with fixed inputs you'd use a variable to > calculate counting the number of questions as the input. For example if > your user was selecting their crop from a select list you could use the > count-selected > function > to determine this. > > Cheers, > Will > > On Mon, Oct 2, 2017 at 6:13 AM, eric.jospe via commcare-users < commcar...@googlegroups.com > wrote: > >> I want to randomly select one crop from a list to ask questions about. I >> know it's possible to use the RANDOM function and set display conditions >> based on that number when the list of options if fixed, but what about when >> the crops that farmers plant vary from case to case? For example, >> farmers may be growing any combination of maize, potatoes, and two types of >> beans (so 1-4 crops). Based on the crops that they’re growing, we want to >> randomly select one crop to ask questions about. >> >> >> Thanks! >> >> >> Eric >> >> -- >> You received this message because you are subscribed to the Google Groups >> "commcare-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to commcare-user...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > >

Let me be more specific. Let's say I have a checkbox with 4 crop choices,
and 4 groups (one for each crop) with questions in them. Based on the crops
selected, I want one group randomly selected. So I'd need display
conditions on the groups so that one is selected at random based on whether
or not that crop was selected. Count-selected only tells me the number of
options selected, not the specific ones.

I might not be understanding what you mean when you say "generating random
with fixed inputs". What I am familiar with is generating a random number,
and then using display conditions to filter based off that number (so if
there are 4 options, the display conditions would be <.25, .25-.5, .5-.75,

.75).

Thanks,

Eric

··· On Tuesday, October 3, 2017 at 1:55:44 PM UTC+2, eric....@oneacrefund.org wrote: > > Hey Will, > > Could you please be more specific? What is the actual syntax I should use > on the hidden value and the display conditions for the questions? > > Thanks, > > Eric > > On Monday, October 2, 2017 at 12:24:57 PM UTC+2, William Pride wrote: >> >> Eric, >> >> You should be able to do this using the same pattern as you would for >> displaying a random question from a fixed set. The only change would be >> that instead of generating random with fixed inputs you'd use a variable to >> calculate counting the number of questions as the input. For example if >> your user was selecting their crop from a select list you could use the >> count-selected >> function >> to determine this. >> >> Cheers, >> Will >> >> On Mon, Oct 2, 2017 at 6:13 AM, eric.jospe via commcare-users < commcar...@googlegroups.com> wrote: >> >>> I want to randomly select one crop from a list to ask questions about. I >>> know it's possible to use the RANDOM function and set display conditions >>> based on that number when the list of options if fixed, but what about when >>> the crops that farmers plant vary from case to case? For example, >>> farmers may be growing any combination of maize, potatoes, and two types of >>> beans (so 1-4 crops). Based on the crops that they’re growing, we want to >>> randomly select one crop to ask questions about. >>> >>> >>> Thanks! >>> >>> >>> Eric >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "commcare-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to commcare-user...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >>

Eric,

Taking your example of four crop choices and giving that question the path
EG /data/select_crops:

/data/number_selected = count-selected(/data/select_crops) # gives you the
number of selected values (we'll say 3 were selected)
/data/random_index = int(random() * /data/number_selected) # gives you an
integer between 0 and the number of selected values - 1
/data/random_crop = selected-at
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-selected-at.1(/data/select_crops,
/data/random_index) # gives you the crop at that index

Then you can use /data/random_crop in your display conditions.

Cheers,
Will

··· On Wed, Oct 4, 2017 at 1:54 AM, eric.jospe via commcare-users < commcare-users@googlegroups.com> wrote:

Let me be more specific. Let's say I have a checkbox with 4 crop choices,
and 4 groups (one for each crop) with questions in them. Based on the crops
selected, I want one group randomly selected. So I'd need display
conditions on the groups so that one is selected at random based on whether
or not that crop was selected. Count-selected only tells me the number of
options selected, not the specific ones.

I might not be understanding what you mean when you say "generating random
with fixed inputs". What I am familiar with is generating a random number,
and then using display conditions to filter based off that number (so if
there are 4 options, the display conditions would be <.25, .25-.5, .5-.75,

.75).

Thanks,

Eric

On Tuesday, October 3, 2017 at 1:55:44 PM UTC+2, eric....@oneacrefund.org wrote:

Hey Will,

Could you please be more specific? What is the actual syntax I should use
on the hidden value and the display conditions for the questions?

Thanks,

Eric

On Monday, October 2, 2017 at 12:24:57 PM UTC+2, William Pride wrote:

Eric,

You should be able to do this using the same pattern as you would for
displaying a random question from a fixed set. The only change would be
that instead of generating random with fixed inputs you'd use a variable to
calculate counting the number of questions as the input. For example if
your user was selecting their crop from a select list you could use the
count-selected
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-count-selected function
to determine this.

Cheers,
Will

On Mon, Oct 2, 2017 at 6:13 AM, eric.jospe via commcare-users < commcar...@googlegroups.com> wrote:

I want to randomly select one crop from a list to ask questions about.
I know it's possible to use the RANDOM function and set display conditions
based on that number when the list of options if fixed, but what about when
the crops that farmers plant vary from case to case? For example,
farmers may be growing any combination of maize, potatoes, and two types of
beans (so 1-4 crops). Based on the crops that they’re growing, we want to
randomly select one crop to ask questions about.

Thanks!

Eric

--
You received this message because you are subscribed to the Google
Groups "commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to commcare-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hey Will,

I created those hidden values and it's now giving me a calculation error
before the survey is able to open: "Error in calculation for
/data/random_crop - attempting to select element 0 of a list with only 0
elements".

Meanwhile, the form has become a little bit more complicated. Instead of a
checkbox with choices of crops, we have a question asking how many fields a
farmer has planted on, and repeat groups for each field. The repeat groups
include the following questions:

  • What was the primary crop planted on this field? <---there are many
    choices, but we want to know whether one of the 4 core crops were selected
    (maize, climbing beans, bush beans, and potatoes)
  • Are you able to harvest this crop?

For all fields that have one of those 4 crops as the primary crop AND where
the crop is harvestable, we want CommCare to randomly select one. I assume
I have to first create a hidden value that counts the fields that meet
those criteria in all repeat groups, and then set up the randomization
questions.

Could you please advise on a way forward?

Thanks!

Eric

··· On Wednesday, October 4, 2017 at 2:18:26 PM UTC+2, William Pride wrote: > > Eric, > > Taking your example of four crop choices and giving that question the path > EG /data/select_crops: > > /data/number_selected = count-selected(/data/select_crops) # gives you the > number of selected values (we'll say 3 were selected) > /data/random_index = int(random() * /data/number_selected) # gives you an > integer between 0 and the number of selected values - 1 > /data/random_crop = selected-at > (/data/select_crops, > /data/random_index) # gives you the crop at that index > > Then you can use /data/random_crop in your display conditions. > > Cheers, > Will > > On Wed, Oct 4, 2017 at 1:54 AM, eric.jospe via commcare-users < commcar...@googlegroups.com > wrote: > >> Let me be more specific. Let's say I have a checkbox with 4 crop choices, >> and 4 groups (one for each crop) with questions in them. Based on the crops >> selected, I want one group randomly selected. So I'd need display >> conditions on the groups so that one is selected at random based on whether >> or not that crop was selected. Count-selected only tells me the number of >> options selected, not the specific ones. >> >> I might not be understanding what you mean when you say "generating >> random with fixed inputs". What I am familiar with is generating a random >> number, and then using display conditions to filter based off that number >> (so if there are 4 options, the display conditions would be <.25, .25-.5, >> .5-.75, >.75). >> >> Thanks, >> >> Eric >> >> >> On Tuesday, October 3, 2017 at 1:55:44 PM UTC+2, eric....@oneacrefund.org wrote: >>> >>> Hey Will, >>> >>> Could you please be more specific? What is the actual syntax I should >>> use on the hidden value and the display conditions for the questions? >>> >>> Thanks, >>> >>> Eric >>> >>> On Monday, October 2, 2017 at 12:24:57 PM UTC+2, William Pride wrote: >>>> >>>> Eric, >>>> >>>> You should be able to do this using the same pattern as you would for >>>> displaying a random question from a fixed set. The only change would be >>>> that instead of generating random with fixed inputs you'd use a variable to >>>> calculate counting the number of questions as the input. For example if >>>> your user was selecting their crop from a select list you could use the >>>> count-selected >>>> function >>>> to determine this. >>>> >>>> Cheers, >>>> Will >>>> >>>> On Mon, Oct 2, 2017 at 6:13 AM, eric.jospe via commcare-users < commcar...@googlegroups.com> wrote: >>>> >>>>> I want to randomly select one crop from a list to ask questions about. >>>>> I know it's possible to use the RANDOM function and set display conditions >>>>> based on that number when the list of options if fixed, but what about when >>>>> the crops that farmers plant vary from case to case? For example, >>>>> farmers may be growing any combination of maize, potatoes, and two types of >>>>> beans (so 1-4 crops). Based on the crops that they’re growing, we want to >>>>> randomly select one crop to ask questions about. >>>>> >>>>> >>>>> Thanks! >>>>> >>>>> >>>>> Eric >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "commcare-users" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to commcare-user...@googlegroups.com. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> >>>> -- >> You received this message because you are subscribed to the Google Groups >> "commcare-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to commcare-user...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > >

Just in case anyone out there wants to use this for their applications,
there was one final step necessary for this to work:

"Leaving the hidden values number_selected and random_index as you
currently have them configured, some small changes to random_crop enables
CommCare to randomly select one of the crops selected in the checkbox
question select_crops and display it in the label.

CommCare gave an error because as the hidden value random_crop fires when
the form opens, the selected-at function recognizes that the select_crops
question was empty (not answered yet). To get around this, we can set the
Calculate Condition for random_crop to:

if(count-selected(#form/select_crops) > 0, selected-at(#form/select_crops,
#form/random_index), '')

This acknowledges that the selected-at function will only calculate when at
least one answer option is selected for select_crops, so the form is able
to open without the error, and the select_crops checkbox question is
required so it will never be blank, thus the if statement will never
calculate to null."

··· On Friday, October 6, 2017 at 8:54:48 AM UTC+2, eric....@oneacrefund.org wrote: > > Hey Will, > > I created those hidden values and it's now giving me a calculation error > before the survey is able to open: "Error in calculation for > /data/random_crop - attempting to select element 0 of a list with only 0 > elements". > > Meanwhile, the form has become a little bit more complicated. Instead of a > checkbox with choices of crops, we have a question asking how many fields a > farmer has planted on, and repeat groups for each field. The repeat groups > include the following questions: > > - What was the primary crop planted on this field? <---there are many > choices, but we want to know whether one of the 4 core crops were selected > (maize, climbing beans, bush beans, and potatoes) > - Are you able to harvest this crop? > > For all fields that have one of those 4 crops as the primary crop AND > where the crop is harvestable, we want CommCare to randomly select one. I > assume I have to first create a hidden value that counts the fields that > meet those criteria in all repeat groups, and then set up the randomization > questions. > > Could you please advise on a way forward? > > Thanks! > > Eric > > On Wednesday, October 4, 2017 at 2:18:26 PM UTC+2, William Pride wrote: >> >> Eric, >> >> Taking your example of four crop choices and giving that question the >> path EG /data/select_crops: >> >> /data/number_selected = count-selected(/data/select_crops) # gives you >> the number of selected values (we'll say 3 were selected) >> /data/random_index = int(random() * /data/number_selected) # gives you an >> integer between 0 and the number of selected values - 1 >> /data/random_crop = selected-at >> (/data/select_crops, >> /data/random_index) # gives you the crop at that index >> >> Then you can use /data/random_crop in your display conditions. >> >> Cheers, >> Will >> >> On Wed, Oct 4, 2017 at 1:54 AM, eric.jospe via commcare-users < commcar...@googlegroups.com> wrote: >> >>> Let me be more specific. Let's say I have a checkbox with 4 crop >>> choices, and 4 groups (one for each crop) with questions in them. Based on >>> the crops selected, I want one group randomly selected. So I'd need display >>> conditions on the groups so that one is selected at random based on whether >>> or not that crop was selected. Count-selected only tells me the number of >>> options selected, not the specific ones. >>> >>> I might not be understanding what you mean when you say "generating >>> random with fixed inputs". What I am familiar with is generating a random >>> number, and then using display conditions to filter based off that number >>> (so if there are 4 options, the display conditions would be <.25, .25-.5, >>> .5-.75, >.75). >>> >>> Thanks, >>> >>> Eric >>> >>> >>> On Tuesday, October 3, 2017 at 1:55:44 PM UTC+2, eric....@oneacrefund.org wrote: >>>> >>>> Hey Will, >>>> >>>> Could you please be more specific? What is the actual syntax I should >>>> use on the hidden value and the display conditions for the questions? >>>> >>>> Thanks, >>>> >>>> Eric >>>> >>>> On Monday, October 2, 2017 at 12:24:57 PM UTC+2, William Pride wrote: >>>>> >>>>> Eric, >>>>> >>>>> You should be able to do this using the same pattern as you would for >>>>> displaying a random question from a fixed set. The only change would be >>>>> that instead of generating random with fixed inputs you'd use a variable to >>>>> calculate counting the number of questions as the input. For example if >>>>> your user was selecting their crop from a select list you could use the >>>>> count-selected >>>>> function >>>>> to determine this. >>>>> >>>>> Cheers, >>>>> Will >>>>> >>>>> On Mon, Oct 2, 2017 at 6:13 AM, eric.jospe via commcare-users < commcar...@googlegroups.com> wrote: >>>>> >>>>>> I want to randomly select one crop from a list to ask questions >>>>>> about. I know it's possible to use the RANDOM function and set display >>>>>> conditions based on that number when the list of options if fixed, but what >>>>>> about when the crops that farmers plant vary from case to case? For >>>>>> example, farmers may be growing any combination of maize, potatoes, and two >>>>>> types of beans (so 1-4 crops). Based on the crops that they’re growing, we >>>>>> want to randomly select one crop to ask questions about. >>>>>> >>>>>> >>>>>> Thanks! >>>>>> >>>>>> >>>>>> Eric >>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "commcare-users" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to commcare-user...@googlegroups.com. >>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>> >>>>> >>>>> -- >>> You received this message because you are subscribed to the Google >>> Groups "commcare-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to commcare-user...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >>

Thanks for closing the loop with that, Eric!

Cheers,
Will

··· On Thu, Oct 12, 2017 at 3:14 AM, eric.jospe via commcare-users < commcare-users@googlegroups.com> wrote:

Just in case anyone out there wants to use this for their applications,
there was one final step necessary for this to work:

"Leaving the hidden values number_selected and random_index as you
currently have them configured, some small changes to random_crop enables
CommCare to randomly select one of the crops selected in the checkbox
question select_crops and display it in the label.

CommCare gave an error because as the hidden value random_crop fires when
the form opens, the selected-at function recognizes that the select_crops
question was empty (not answered yet). To get around this, we can set the
Calculate Condition for random_crop to:

if(count-selected(#form/select_crops) > 0, selected-at(#form/select_crops,
#form/random_index), '')

This acknowledges that the selected-at function will only calculate when
at least one answer option is selected for select_crops, so the form is
able to open without the error, and the select_crops checkbox question is
required so it will never be blank, thus the if statement will never
calculate to null."

On Friday, October 6, 2017 at 8:54:48 AM UTC+2, eric....@oneacrefund.org wrote:

Hey Will,

I created those hidden values and it's now giving me a calculation error
before the survey is able to open: "Error in calculation for
/data/random_crop - attempting to select element 0 of a list with only 0
elements".

Meanwhile, the form has become a little bit more complicated. Instead of
a checkbox with choices of crops, we have a question asking how many fields
a farmer has planted on, and repeat groups for each field. The repeat
groups include the following questions:

  • What was the primary crop planted on this field? <---there are many
    choices, but we want to know whether one of the 4 core crops were selected
    (maize, climbing beans, bush beans, and potatoes)
  • Are you able to harvest this crop?

For all fields that have one of those 4 crops as the primary crop AND
where the crop is harvestable, we want CommCare to randomly select one. I
assume I have to first create a hidden value that counts the fields that
meet those criteria in all repeat groups, and then set up the randomization
questions.

Could you please advise on a way forward?

Thanks!

Eric

On Wednesday, October 4, 2017 at 2:18:26 PM UTC+2, William Pride wrote:

Eric,

Taking your example of four crop choices and giving that question the
path EG /data/select_crops:

/data/number_selected = count-selected(/data/select_crops) # gives you
the number of selected values (we'll say 3 were selected)
/data/random_index = int(random() * /data/number_selected) # gives you
an integer between 0 and the number of selected values - 1
/data/random_crop = selected-at
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-selected-at.1
(/data/select_crops, /data/random_index) # gives you the crop at that
index

Then you can use /data/random_crop in your display conditions.

Cheers,
Will

On Wed, Oct 4, 2017 at 1:54 AM, eric.jospe via commcare-users < commcar...@googlegroups.com> wrote:

Let me be more specific. Let's say I have a checkbox with 4 crop
choices, and 4 groups (one for each crop) with questions in them. Based on
the crops selected, I want one group randomly selected. So I'd need display
conditions on the groups so that one is selected at random based on whether
or not that crop was selected. Count-selected only tells me the number of
options selected, not the specific ones.

I might not be understanding what you mean when you say "generating
random with fixed inputs". What I am familiar with is generating a random
number, and then using display conditions to filter based off that number
(so if there are 4 options, the display conditions would be <.25, .25-.5,
.5-.75, >.75).

Thanks,

Eric

On Tuesday, October 3, 2017 at 1:55:44 PM UTC+2, eric....@oneacrefund.org wrote:

Hey Will,

Could you please be more specific? What is the actual syntax I should
use on the hidden value and the display conditions for the questions?

Thanks,

Eric

On Monday, October 2, 2017 at 12:24:57 PM UTC+2, William Pride wrote:

Eric,

You should be able to do this using the same pattern as you would for
displaying a random question from a fixed set. The only change would be
that instead of generating random with fixed inputs you'd use a variable to
calculate counting the number of questions as the input. For example if
your user was selecting their crop from a select list you could use the
count-selected
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-count-selected function
to determine this.

Cheers,
Will

On Mon, Oct 2, 2017 at 6:13 AM, eric.jospe via commcare-users < commcar...@googlegroups.com> wrote:

I want to randomly select one crop from a list to ask questions
about. I know it's possible to use the RANDOM function and set display
conditions based on that number when the list of options if fixed, but what
about when the crops that farmers plant vary from case to case? For
example, farmers may be growing any combination of maize, potatoes, and two
types of beans (so 1-4 crops). Based on the crops that they’re growing, we
want to randomly select one crop to ask questions about.

Thanks!

Eric

--
You received this message because you are subscribed to the Google
Groups "commcare-users" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to commcare-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google
Groups "commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to commcare-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.