Randomly Select a Household Member in XLSForm
To pick a random household member in XLSForm, store one random number with once(random()), then read one entry from a list of eligible roster numbers with selected-at(${adult_list}, int(${draw} * ${n_adults})). The form makes the choice, not the enumerator, and every eligible member has the same chance. Each example below compiles with pyxform and ODK Validate, the same check ODK Central runs when you upload a form.
How do I randomly select one adult from a household roster?
Four pieces do the work: a marker inside the roster, a list of the eligible roster numbers, one stored random number, and a lookup.
| type | name | label | calculation | relevant |
|---|---|---|---|---|
| begin_repeat | member | Household member | ||
| text | member_name | Name of household member | ||
| integer | member_age | Age of ${member_name} in completed years | ||
| calculate | adult_pos | if(${member_age} >= 18, position(..), '') | ||
| end_repeat | ||||
| calculate | adult_list | join(' ', ${adult_pos}[. != '']) | ||
| calculate | n_adults | count-selected(${adult_list}) | ||
| calculate | draw | once(random()) | ||
| calculate | pick_index | int(${draw} * ${n_adults}) | ||
| calculate | pick_pos | selected-at(${adult_list}, ${pick_index}) | ||
| calculate | pick_name | ${member}[position() = ${pick_pos}]/member_name | ||
| note | show_pick | Interview ${pick_name} (member ${pick_pos} of the roster; ${n_adults} adults in the household). | ${n_adults} > 0 | |
| note | no_adult | No adult is listed in this household. | ${n_adults} = 0 |
Row by row:
adult_posruns once per member.position(..)is the member's number in the roster, so an adult entered third gets3and a child gets nothing.adult_listjoins those numbers into one text, such as1 3 4. The filter[. != '']leaves out the empty values of members who are not eligible.n_adultscounts the entries in the list withcount-selected().drawis a random number from 0 up to, but not including, 1.once()stores it the first time and never replaces it.pick_indexscales the draw to the list. With three adults,int(${draw} * 3)is 0, 1 or 2, each with the same chance.pick_posreads that entry from the list.selected-at()counts from 0, which is why there is no+ 1.pick_namereads the name of the roster member at that position. This filter on the repeat returns nothing when no adult is listed, where indexed-repeat() would need a valid number.
The draw is fixed, but the pick follows the roster until the roster is complete. That is what you want: the form cannot know the pick before it knows how many adults there are. Train enumerators to finish the roster before they move on, and not to add or remove members afterwards.
Keep once(random()) in a row of its own. If you wrap the whole pick in once(), the form stores the pick as soon as the first eligible member is entered and never updates it. A bare random() with no once() has the opposite problem: the selected person can change during the interview.
How do I select from members who meet more than one condition?
Put every condition in the marker inside the repeat. The rest of the pattern does not change. This form selects one woman aged 15 to 49 for a women's questionnaire, then asks for her consent by name:
| type | name | label | calculation | relevant |
|---|---|---|---|---|
| begin_repeat | person | Household member | ||
| text | person_name | Name of household member | ||
| select_one sex | person_sex | Sex of ${person_name} | ||
| integer | person_age | Age of ${person_name} in completed years | ||
| calculate | wra_pos | if(${person_sex} = '2' and ${person_age} >= 15 and ${person_age} <= 49, position(..), '') | ||
| end_repeat | ||||
| calculate | wra_list | join(' ', ${wra_pos}[. != '']) | ||
| calculate | n_wra | count-selected(${wra_list}) | ||
| calculate | wra_draw | once(random()) | ||
| calculate | wra_pick_pos | selected-at(${wra_list}, int(${wra_draw} * ${n_wra})) | ||
| calculate | wra_name | ${person}[position() = ${wra_pick_pos}]/person_name | ||
| note | no_wra | No woman aged 15 to 49 lives in this household. Skip the women's questionnaire. | ${n_wra} = 0 | |
| select_one yes_no | wra_consent | ${wra_name} was selected for the women's questionnaire. Does she agree to be interviewed? | ${n_wra} > 0 |
| list_name | name | label |
|---|---|---|
| sex | 1 | Male |
| sex | 2 | Female |
| yes_no | 1 | Yes |
| yes_no | 0 | No |
The same marker selects a child aged 5 to 15, a boy, a caregiver or a household member who worked last week: change the condition, keep the other rows. The conditions use and and or exactly as in an XLSForm if statement.
Always plan for a household with nobody eligible. Here n_wra is 0, the note explains what to do, and the consent question stays hidden. Put the same ${n_wra} > 0 condition on the group that holds the women's questionnaire.
How do I select a second member without picking the same person twice?
Surveys often need a backup respondent, or two people from the same household. Two independent draws can land on the same person. The fix is to draw the second pick from the remaining members: choose among one fewer, then step over the first pick.
| type | name | label | calculation | relevant |
|---|---|---|---|---|
| begin_repeat | resident | Household member | ||
| text | resident_name | Name of household member | ||
| integer | resident_age | Age of ${resident_name} in completed years | ||
| calculate | elig_pos | if(${resident_age} >= 18, position(..), '') | ||
| end_repeat | ||||
| calculate | elig_list | join(' ', ${elig_pos}[. != '']) | ||
| calculate | n_elig | count-selected(${elig_list}) | ||
| calculate | draw_1 | once(random()) | ||
| calculate | draw_2 | once(random()) | ||
| calculate | index_1 | int(${draw_1} * ${n_elig}) | ||
| calculate | index_2_raw | int(${draw_2} * (${n_elig} - 1)) | ||
| calculate | index_2 | if(${index_2_raw} >= ${index_1}, ${index_2_raw} + 1, ${index_2_raw}) | ||
| calculate | first_pos | selected-at(${elig_list}, ${index_1}) | ||
| calculate | backup_pos | if(${n_elig} >= 2, selected-at(${elig_list}, ${index_2}), '') | ||
| calculate | first_name | ${resident}[position() = ${first_pos}]/resident_name | ||
| calculate | backup_name | ${resident}[position() = ${backup_pos}]/resident_name | ||
| note | show_first | Selected respondent: ${first_name}. | ${n_elig} >= 1 | |
| note | show_backup | If ${first_name} cannot be interviewed after the agreed number of visits, interview ${backup_name}. | ${n_elig} >= 2 |
With four eligible adults, index_1 is one of 0 to 3 and index_2_raw is one of 0 to 2. If the raw value is at or past the first pick, it moves up by one, so the two picks are always different and every remaining adult has the same chance of being the backup.
Whether a backup is allowed at all is a sampling decision, not a form decision. Replacing a respondent who is hard to reach biases the sample towards people who are at home, so many protocols require call-backs and no substitution. Follow your survey's protocol and use this pattern only where it calls for one.
How do I pick a random member without a roster?
When the questionnaire has no roster, ask how many eligible people there are and let the form name a number. The enumerator lists the adults in a fixed order and interviews the one with that number:
| type | name | label | calculation | constraint |
|---|---|---|---|---|
| integer | n_adults_home | How many adults aged 18 or over live in this household? | . >= 1 and . <= 20 | |
| calculate | kish_draw | once(random()) | ||
| calculate | kish_number | int(${kish_draw} * ${n_adults_home}) + 1 | ||
| note | kish_note | List the adults from oldest to youngest. Interview adult number ${kish_number}. |
Here the result is a number shown to a person, so it counts from 1 and needs the + 1. The order must be fixed by a rule (oldest to youngest is the usual one) and the list written down before the note appears, otherwise the enumerator can arrange the list to suit whoever opened the door.
Is this the same as a Kish grid?
It does the same job. A Kish grid is a printed table: the enumerator lists the eligible adults in a set order, then reads the person to interview from the row for the household's size and the column assigned to that questionnaire. It was designed for paper, where the form cannot draw a random number.
On a phone or tablet, the draw replaces the table. You still need two things from the Kish method:
- A complete list first. The roster, or the count, comes before the selection is shown.
- The number of eligible members in the data. One adult from a household of four had a 1 in 4 chance of selection, so analysts weight that interview by 4. In the examples above,
n_adults,n_wraandn_eligare saved with the submission for this purpose.
The stored draw is also in the data, so a supervisor can recompute the pick from the roster and confirm that the right person was interviewed.
Common mistakes and how to fix them
We ran the broken versions through pyxform and ODK Validate. The messages are what you will see in ODK Central, KoboToolbox or the XLSForm validator:
| You wrote | Write instead | Error you will see |
|---|---|---|
RAND() or randbetween(1, ${n}) | once(random()), then int(${draw} * ${n}) + 1 | cannot handle function 'RAND' / cannot handle function 'randbetween' |
random(1, ${n}) | int(${draw} * ${n}) + 1 | cannot handle function 'random'. Requires 0 arguments but 2 provided. |
selected_at(...) | selected-at(...) | cannot handle function 'selected_at' |
selected-at(${adult_list}) | selected-at(${adult_list}, ${pick_index}) | cannot handle function 'selected-at'. Requires 2 arguments but 1 provided. |
once(int(random() * ${n}) + 1 | Close every bracket | Mismatched brackets or parentheses in expression |
int(random() * ${n}) with no once() | Store once(random()) in its own row | No error, but the selected person can change |
selected-at(${adult_list}, ${pick_index} + 1) | Use the index as it is; it counts from 0 | No error, but the first adult is never picked and the last pick is blank |
count(${adult_pos}) for the number of adults | count-selected(${adult_list}) | No error, but it counts every roster member |
The last three rows compile and look fine in a quick test with one or two members. Test with a household of four or five, including members who are not eligible, and check the note against the roster. For other messages, see common XLSForm errors.
Does this work the same in ODK, KoboToolbox and SurveyCTO?
The functions are the same on all three: random(), once(), join(), selected-at() and count-selected(). What differs is the roster number and where the logic is typed:
| Platform | What to know |
|---|---|
| ODK Collect and Central | Works as written. The roster number is position(..) |
| KoboToolbox (XLSForm upload) | Same syntax. Test the form in KoboCollect and in the Enketo web form with several members before fieldwork |
| KoboToolbox Formbuilder | Add Calculate questions and type the expressions in their calculation boxes; uploading the XLSForm is easier for a pattern with this many rows |
| SurveyCTO | Use index() for the roster number instead of position(..). Skip logic goes in a column named relevance. The ${member}[...] filters are ODK XPath, so check SurveyCTO's documentation for its own way to read from a repeat |
The no-roster example uses only once(), random() and int(), and moves between the three platforms unchanged apart from the column name for skip logic.
Why the form should make the selection
Left to choose, an enumerator interviews whoever is at home and willing. That is more often someone who is home during the day, and the sample drifts away from the population. A selection made by the form removes the choice, and a stored draw makes it checkable. For more on building the roster itself, see repeat groups in XLSForm; to shuffle choices or question blocks rather than people, see how to randomize questions.
You can describe the rule in plain language: "after the roster, randomly select one woman aged 15 to 49 and ask the health module about her by name". LooprAI writes the marker, the list, the stored draw and the lookup, then compiles the form with pyxform before anything is saved.
Next: repeat groups in XLSForm →
Frequently Asked Questions
How do I randomly select a household member in XLSForm?
Mark eligible members inside the roster repeat, join their roster numbers into a list, store once(random()) in a calculate, then read one entry with selected-at(${list}, int(${draw} * ${count})). Use that roster number to show the selected member's name.
Why does the randomly selected person change during the interview?
The form uses random() without once(). random() can be evaluated again when the form is reopened or an answer changes, giving a new number each time. Store once(random()) in its own calculate row and build the pick from that stored value.
How do I randomly select only from eligible members, such as adults?
Put the eligibility rule in a calculate inside the repeat: if(${member_age} >= 18, position(..), ''). After the repeat, join the non-empty values into a list. The random pick reads from that list, so members who are not eligible can never be selected.
Can I select two household members at random without repeating one?
Yes. Draw the first index from all eligible members and the second from one fewer, then add 1 to the second index when it is at or past the first. The two picks are always different, and each remaining member has an equal chance.
Do I still need a Kish grid with ODK or KoboToolbox?
No. A Kish grid is a printed table for paper questionnaires. In a digital form, a stored random draw selects the person with equal probability. Keep the two habits from the Kish method: list everyone first, and save the number of eligible members for weighting.
Does random selection work the same in KoboToolbox and SurveyCTO?
The functions random(), once(), join() and selected-at() are available on ODK, KoboToolbox and SurveyCTO. KoboToolbox uses the same syntax as ODK. In SurveyCTO, use index() for the roster number instead of position(..), and the relevance column for skip logic.
No Credit Card Required
Build the form first. Add hosting when you need it.
Start free with LooprAI, then deploy a managed ODK Central server or run DataSnap checks when your project is ready.
Automation handles the mechanical work.
Researchers decide what the evidence means.