# Select an 5-15 years old household member randomly

Hi guys,
I have a big challenge to select randomly a 5-15 years old household
member. I have a questionnaire with two sections. the first section ask
questions about democraphics, economics for each member. In the second
second, we have to select randomly a 5-15 years old to ask specifics
questions. I don't know how can I do it in odk? Can sombody tell me how
thanks a lot

You would first want to limit your survey to a maximum of N household
members in the designated age range (N = 10 ?).

You would have a training issue to ensure that interviewers never edit /
delete or alter the set of household members once they complete that
section of the survey. ODK Collect cannot handle that well.

The general flow is:

Ask for basic demographics in a repeat group.

Then have a series of calculated field that produce a space-separated list
of the indices into that repeat group that are within the designated age
range.

I would probably have a set of N fields that held '' or ' K' based upon
whether or not that index was within the age group. e.g.,

And then have a calculated field that smashes these together, removing the

You can now use once(), random() and the functions available for
select-multiple values to extract a random selection from this set.

The above only computes the chosen member once, via the once() function
added in ODK Collect 1.4.3 -- if you re-edit the form, you don't have to
worry about the chosen member changing.

Within that function, we use the count-selected() function to determine how
many household members are within the designated age range.

If there aren't any, we set chosenmember to zero (0).

Otherwise, we compute a random() number between 0 and 0.99999999 and
multiply this by the number of members within the age range. This gives,
e.g., for 3 children:

0 .. 2.99999

We then add 1.0 to this, and convert the value to an integer. This yields
an integer in the set (1, 2, 3).

You can then have a

relevant="/data/chosenmenber > 0"

to handle the case where no children are present and use

/data/member[/data/chosenmember]/name

to reference the child's name in subsequent questions.

··· ---------------- Mitch

On Sat, Jun 7, 2014 at 2:16 PM, Legarraudien pagarraud1@gmail.com wrote:

Hi guys,
I have a big challenge to select randomly a 5-15 years old household
member. I have a questionnaire with two sections. the first section ask
questions about democraphics, economics for each member. In the second
second, we have to select randomly a 5-15 years old to ask specifics
questions. I don't know how can I do it in odk? Can sombody tell me how
thanks a lot

## --

You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

1 Like

Hi Mitch

I hard try to follow your instruction, but finally i did'nt reference any
child

here my xlsform

garraud.xls (27 KB)

··· Le samedi 7 juin 2014 14:16:28 UTC-7, Legarraudien a écrit : > > Hi guys, > I have a big challenge to select randomly a 5-15 years old household > member. I have a questionnaire with two sections. the first section ask > questions about democraphics, economics for each member. In the second > second, we have to select randomly a 5-15 years old to ask specifics > questions. I don't know how can I do it in odk? Can sombody tell me how > can adresse this issue? > thanks a lot >

I think i attached the wrong file,

garraud.xls (27 KB)

··· Le samedi 7 juin 2014 14:16:28 UTC-7, Legarraudien a écrit : > > Hi guys, > I have a big challenge to select randomly a 5-15 years old household > member. I have a questionnaire with two sections. the first section ask > questions about democraphics, economics for each member. In the second > second, we have to select randomly a 5-15 years old to ask specifics > questions. I don't know how can I do it in odk? Can sombody tell me how > can adresse this issue? > thanks a lot >

Hi Mitch,
I thank you for precious answer. It seems to be more dfficult to apply
this solution direct in a xml file. How can i do it in an excel file. I
think it will be very easy. Otherwise, can you tell how can i implent it
directy in the xml file

··· 2014-06-09 10:23 GMT-07:00 Mitch Sundt :

You would first want to limit your survey to a maximum of N household
members in the designated age range (N = 10 ?).

You would have a training issue to ensure that interviewers never edit /
delete or alter the set of household members once they complete that
section of the survey. ODK Collect cannot handle that well.

The general flow is:

Ask for basic demographics in a repeat group.

Then have a series of calculated field that produce a space-separated list
of the indices into that repeat group that are within the designated age
range.

I would probably have a set of N fields that held '' or ' K' based upon
whether or not that index was within the age group. e.g.,

And then have a calculated field that smashes these together, removing the

You can now use once(), random() and the functions available for
select-multiple values to extract a random selection from this set.

The above only computes the chosen member once, via the once() function
added in ODK Collect 1.4.3 -- if you re-edit the form, you don't have to
worry about the chosen member changing.

Within that function, we use the count-selected() function to determine
how many household members are within the designated age range.

If there aren't any, we set chosenmember to zero (0).

Otherwise, we compute a random() number between 0 and 0.99999999 and
multiply this by the number of members within the age range. This gives,
e.g., for 3 children:

0 .. 2.99999

We then add 1.0 to this, and convert the value to an integer. This yields
an integer in the set (1, 2, 3).

You can then have a

relevant="/data/chosenmenber > 0"

to handle the case where no children are present and use

/data/member[/data/chosenmember]/name

to reference the child's name in subsequent questions.

Mitch

On Sat, Jun 7, 2014 at 2:16 PM, Legarraudien pagarraud1@gmail.com wrote:

Hi guys,
I have a big challenge to select randomly a 5-15 years old household
member. I have a questionnaire with two sections. the first section ask
questions about democraphics, economics for each member. In the second
second, we have to select randomly a 5-15 years old to ask specifics
questions. I don't know how can I do it in odk? Can sombody tell me how
thanks a lot

## --

You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

## --

You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an

I know this is an old post, but my question is related. I have used calculate="int(1.0+count-selected(/data/membersinrange)*random()))" have successfully got the random selection. However, when I use once() function, the first person in the list always gets selected. I am using once functiona like this.
calculate="once(int(1.0+count-selected(/data/membersinrange)*random())))"
but with no success.

I am using the latest version of ODK Collect. Any help will be appreciated.

··· On Sunday, July 13, 2014 at 6:53:21 AM UTC+5:30, Legarraudien wrote: > I think i attached the wrong file, > please fiind attached xlsfom > > > > > > Le samedi 7 juin 2014 14:16:28 UTC-7, Legarraudien a écrit : > Hi guys, > I have a big challenge to select randomly a 5-15 years old household member. I have a questionnaire with two sections. the first section ask questions about democraphics, economics for each member. In the second second, we have to select randomly a 5-15 years old to ask specifics questions. I don't know how can I do it in odk? Can sombody tell me how can adresse this issue? > thanks a lot

I found an easier way to do this. So please check
In this example I want to select the index position of a girl between 5 and 17.
steps.

1. inside the begin repeat a calculate a variable that has the position of the member inside the repeat that matches the condition. i use:
Better you see the attached xls file.

calculate eligibleGirl if(\${age}>4 and \${age}<18 and \${female}=1,position(..),'')

1. Outside the repeat i use the following functions : random(); join(); count-selected(), and selected-at(). once i have the index of the random selected girl i can use indexed-repeat() to get the name or idnumber.

calculate random1 once(random())
note note1 random number is \${random1}
calculate listgirspos join(' ',\${eligibleGirl})
note note2 girls' positions are \${listgirspos}
calculate n_selected count-selected(\${listgirspos})
note note3 Number of girls selected \${n_selected}
calculate selectedGirl selected-at(\${listgirspos},int(\${random1}*\${n_selected}))
note note4 the Girl selected is \${selectedGirl}

Book1.xlsx (9.67 KB)

··· On Saturday, July 12, 2014 at 9:23:21 PM UTC-4, Legarraudien wrote: > I think i attached the wrong file, > please fiind attached xlsfom > > > > > > Le samedi 7 juin 2014 14:16:28 UTC-7, Legarraudien a écrit : > Hi guys, > I have a big challenge to select randomly a 5-15 years old household member. I have a questionnaire with two sections. the first section ask questions about democraphics, economics for each member. In the second second, we have to select randomly a 5-15 years old to ask specifics questions. I don't know how can I do it in odk? Can sombody tell me how can adresse this issue? > thanks a lot

Definitely try to stay in the XLS file as much as possible. When it comes
to the

if(/data/member[1]/age >= 5 and /data/member[1]/age <= 15, ' 1', '')

you will need to replicate whatever path the XLS file emits for the XML,
but other than that, the \${fieldname} syntax should work just fine.

··· On Mon, Jun 9, 2014 at 11:10 AM, Pierre Anthony Garraud < pagarraud1@gmail.com> wrote:

Hi Mitch,
I thank you for precious answer. It seems to be more dfficult to apply
this solution direct in a xml file. How can i do it in an excel file. I
think it will be very easy. Otherwise, can you tell how can i implent it
directy in the xml file

2014-06-09 10:23 GMT-07:00 Mitch Sundt mitchellsundt@gmail.com:

You would first want to limit your survey to a maximum of N household

members in the designated age range (N = 10 ?).

You would have a training issue to ensure that interviewers never edit /
delete or alter the set of household members once they complete that
section of the survey. ODK Collect cannot handle that well.

The general flow is:

Ask for basic demographics in a repeat group.

Then have a series of calculated field that produce a space-separated
list of the indices into that repeat group that are within the designated
age range.

I would probably have a set of N fields that held '' or ' K' based upon
whether or not that index was within the age group. e.g.,

And then have a calculated field that smashes these together, removing

You can now use once(), random() and the functions available for
select-multiple values to extract a random selection from this set.

The above only computes the chosen member once, via the once() function
added in ODK Collect 1.4.3 -- if you re-edit the form, you don't have to
worry about the chosen member changing.

Within that function, we use the count-selected() function to determine
how many household members are within the designated age range.

If there aren't any, we set chosenmember to zero (0).

Otherwise, we compute a random() number between 0 and 0.99999999 and
multiply this by the number of members within the age range. This gives,
e.g., for 3 children:

0 .. 2.99999

We then add 1.0 to this, and convert the value to an integer. This yields
an integer in the set (1, 2, 3).

You can then have a

relevant="/data/chosenmenber > 0"

to handle the case where no children are present and use

/data/member[/data/chosenmember]/name

to reference the child's name in subsequent questions.

Mitch

On Sat, Jun 7, 2014 at 2:16 PM, Legarraudien pagarraud1@gmail.com wrote:

Hi guys,
I have a big challenge to select randomly a 5-15 years old household
member. I have a questionnaire with two sections. the first section ask
questions about democraphics, economics for each member. In the second
second, we have to select randomly a 5-15 years old to ask specifics
questions. I don't know how can I do it in odk? Can sombody tell me how
thanks a lot

## --

Groups "ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

## --

You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an

## --

You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

You should compute and save the value of JUST random().

The count-selected() value will change as you add or remove values, and you
want it to correctly reflect the total after those changes.

Placing count-selected() inside a once() will evaluate it only once (when
it is empty at the time of creating a new submission), which is not what
you want.

I.e.,

random_n calculate="once(random())"
chosen_idx calculate="int(1.0+count-selected(\${membersinrange})*\${
random_n}))"

··· On Mon, Aug 24, 2015 at 11:10 AM, Mandy wrote:

I know this is an old post, but my question is related. I have used
calculate="int(1.0+count-selected(/data/membersinrange)*random()))" have
successfully got the random selection. However, when I use once() function,
the first person in the list always gets selected. I am using once
functiona like this.
calculate="once(int(1.0+count-selected(/data/membersinrange)*random())))"
but with no success.

I am using the latest version of ODK Collect. Any help will be appreciated.

On Sunday, July 13, 2014 at 6:53:21 AM UTC+5:30, Legarraudien wrote:

I think i attached the wrong file,

Le samedi 7 juin 2014 14:16:28 UTC-7, Legarraudien a écrit :
Hi guys,
I have a big challenge to select randomly a 5-15 years old household
member. I have a questionnaire with two sections. the first section ask
questions about democraphics, economics for each member. In the second
second, we have to select randomly a 5-15 years old to ask specifics
questions. I don't know how can I do it in odk? Can sombody tell me how
thanks a lot

## --

You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

Hi,
I really appreciate it. Thanks a lot. I look into the excel files later.

Many thanks
Pierre

··· Le 17 juin 2016 11:02, a écrit :

I found an easier way to do this. So please check
In this example I want to select the index position of a girl between 5
and 17.
steps.

1. inside the begin repeat a calculate a variable that has the position of
the member inside the repeat that matches the condition. i use:
Better you see the attached xls file.

calculate eligibleGirl
if(\${age}>4 and \${age}<18 and
\${female}=1,position(..),'')

1. Outside the repeat i use the following functions : random(); join();
count-selected(), and selected-at(). once i have the index of the random
selected girl i can use indexed-repeat() to get the name or idnumber.

calculate random1
once(random())
note note1 random number is \${random1}
calculate listgirspos
join(' ',\${eligibleGirl})
note note2 girls' positions are \${listgirspos}
calculate n_selected
count-selected(\${listgirspos})
note note3 Number of girls selected \${n_selected}
calculate selectedGirl

selected-at(\${listgirspos},int(\${random1}*\${n_selected}))
note note4 the Girl selected is \${selectedGirl}

On Saturday, July 12, 2014 at 9:23:21 PM UTC-4, Legarraudien wrote:

I think i attached the wrong file,

Le samedi 7 juin 2014 14:16:28 UTC-7, Legarraudien a écrit :
Hi guys,
I have a big challenge to select randomly a 5-15 years old household
member. I have a questionnaire with two sections. the first section ask
questions about democraphics, economics for each member. In the second
second, we have to select randomly a 5-15 years old to ask specifics
questions. I don't know how can I do it in odk? Can sombody tell me how