Randomly Select from a repeat group

Hi,

I am building a household survey form. We will be using a repeat group to
get some basic details of every woman living in that household. I then want
to randomly select one woman from that repeat group and use her as a case
that we administer a lot of follow up forms on.

I have set up a hidden value outside of the repeat group called
"randomly_selected_woman" with a calculate condition of:

/data/Women_in_household/womans_name[0 + int(random() *
(count(/data/Women_in_household) - 0))]

I want to use this value to register a new case, but I can’t seem to get
the value to work. Any suggestions as to what I might be doing wrong?

Thanks
Damian

Hey Damian,

Are you getting an error in your application when you try do this? If its
a hidden value, it may be failing if there are no women_in_household yet
(when the form is first opened). You can add an if statement in your
calculation to handle this situation:

if(count(/data/Women_in_household) > 0, /data/Women_in_household/womans_name[0

  • int(random() * (count(/data/Women_in_household) - 0))])

Sheel

··· On Fri, Apr 10, 2015 at 5:39 AM, Damian Hacking wrote:

Hi,

I am building a household survey form. We will be using a repeat group to
get some basic details of every woman living in that household. I then want
to randomly select one woman from that repeat group and use her as a case
that we administer a lot of follow up forms on.

I have set up a hidden value outside of the repeat group called
"randomly_selected_woman" with a calculate condition of:

/data/Women_in_household/womans_name[0 + int(random() *
(count(/data/Women_in_household) - 0))]

I want to use this value to register a new case, but I can’t seem to get
the value to work. Any suggestions as to what I might be doing wrong?

Thanks
Damian


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.


Sheel Shah
Project Manager | Dimagi
m: +1.781.428.5419 | skype: sheel_shah

Damian,

Our XPath expressions unfortunately don’t currently support the syntactic
sugar for selecting an element’s position in the current expression as you
are doing, ie:

/data/element[1] -> /data/element[position() = 1]

you’ll need to manually use the position() function to evaluate the
selection of the element you want.

Also, I haven’t carefully read your expression (so I’m not sure if you are
doing this) but please do remember that XPath position() lookups are
1-indexed, not 0-indexed.

-Clayton

··· On Fri, Apr 10, 2015 at 5:39 AM, Damian Hacking wrote:

Hi,

I am building a household survey form. We will be using a repeat group to
get some basic details of every woman living in that household. I then want
to randomly select one woman from that repeat group and use her as a case
that we administer a lot of follow up forms on.

I have set up a hidden value outside of the repeat group called
"randomly_selected_woman" with a calculate condition of:

/data/Women_in_household/womans_name[0 + int(random() *
(count(/data/Women_in_household) - 0))]

I want to use this value to register a new case, but I can’t seem to get
the value to work. Any suggestions as to what I might be doing wrong?

Thanks
Damian


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.