SurveyLoopr
Tips & Tricks
12 min read

Randomly Select a Household Member in XLSForm

Mehrab Ali
Mehrab AliPublished November 18, 2026

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.

Download example form (.xlsx)All four examples in one XLSForm, one group each. Tested with pyxform and ODK Validate; free to reuse under CC BY 4.0.Download

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.

typenamelabelcalculationrelevant
begin_repeatmemberHousehold member
textmember_nameName of household member
integermember_ageAge of ${member_name} in completed years
calculateadult_posif(${member_age} >= 18, position(..), '')
end_repeat
calculateadult_listjoin(' ', ${adult_pos}[. != ''])
calculaten_adultscount-selected(${adult_list})
calculatedrawonce(random())
calculatepick_indexint(${draw} * ${n_adults})
calculatepick_posselected-at(${adult_list}, ${pick_index})
calculatepick_name${member}[position() = ${pick_pos}]/member_name
noteshow_pickInterview ${pick_name} (member ${pick_pos} of the roster; ${n_adults} adults in the household).${n_adults} > 0
noteno_adultNo adult is listed in this household.${n_adults} = 0
surveychoicessettings

Row by row:

  • adult_pos runs once per member. position(..) is the member's number in the roster, so an adult entered third gets 3 and a child gets nothing.
  • adult_list joins those numbers into one text, such as 1 3 4. The filter [. != ''] leaves out the empty values of members who are not eligible.
  • n_adults counts the entries in the list with count-selected().
  • draw is a random number from 0 up to, but not including, 1. once() stores it the first time and never replaces it.
  • pick_index scales the draw to the list. With three adults, int(${draw} * 3) is 0, 1 or 2, each with the same chance.
  • pick_pos reads that entry from the list. selected-at() counts from 0, which is why there is no + 1.
  • pick_name reads 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.

Warning:

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:

typenamelabelcalculationrelevant
begin_repeatpersonHousehold member
textperson_nameName of household member
select_one sexperson_sexSex of ${person_name}
integerperson_ageAge of ${person_name} in completed years
calculatewra_posif(${person_sex} = '2' and ${person_age} >= 15 and ${person_age} <= 49, position(..), '')
end_repeat
calculatewra_listjoin(' ', ${wra_pos}[. != ''])
calculaten_wracount-selected(${wra_list})
calculatewra_drawonce(random())
calculatewra_pick_posselected-at(${wra_list}, int(${wra_draw} * ${n_wra}))
calculatewra_name${person}[position() = ${wra_pick_pos}]/person_name
noteno_wraNo woman aged 15 to 49 lives in this household. Skip the women's questionnaire.${n_wra} = 0
select_one yes_nowra_consent${wra_name} was selected for the women's questionnaire. Does she agree to be interviewed?${n_wra} > 0
surveychoicessettings
list_namenamelabel
sex1Male
sex2Female
yes_no1Yes
yes_no0No
surveychoicessettings

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.

typenamelabelcalculationrelevant
begin_repeatresidentHousehold member
textresident_nameName of household member
integerresident_ageAge of ${resident_name} in completed years
calculateelig_posif(${resident_age} >= 18, position(..), '')
end_repeat
calculateelig_listjoin(' ', ${elig_pos}[. != ''])
calculaten_eligcount-selected(${elig_list})
calculatedraw_1once(random())
calculatedraw_2once(random())
calculateindex_1int(${draw_1} * ${n_elig})
calculateindex_2_rawint(${draw_2} * (${n_elig} - 1))
calculateindex_2if(${index_2_raw} >= ${index_1}, ${index_2_raw} + 1, ${index_2_raw})
calculatefirst_posselected-at(${elig_list}, ${index_1})
calculatebackup_posif(${n_elig} >= 2, selected-at(${elig_list}, ${index_2}), '')
calculatefirst_name${resident}[position() = ${first_pos}]/resident_name
calculatebackup_name${resident}[position() = ${backup_pos}]/resident_name
noteshow_firstSelected respondent: ${first_name}.${n_elig} >= 1
noteshow_backupIf ${first_name} cannot be interviewed after the agreed number of visits, interview ${backup_name}.${n_elig} >= 2
surveychoicessettings

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:

typenamelabelcalculationconstraint
integern_adults_homeHow many adults aged 18 or over live in this household?. >= 1 and . <= 20
calculatekish_drawonce(random())
calculatekish_numberint(${kish_draw} * ${n_adults_home}) + 1
notekish_noteList the adults from oldest to youngest. Interview adult number ${kish_number}.
surveychoicessettings

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_wra and n_elig are 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 wroteWrite insteadError you will see
RAND() or randbetween(1, ${n})once(random()), then int(${draw} * ${n}) + 1cannot handle function 'RAND' / cannot handle function 'randbetween'
random(1, ${n})int(${draw} * ${n}) + 1cannot 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}) + 1Close every bracketMismatched brackets or parentheses in expression
int(random() * ${n}) with no once()Store once(random()) in its own rowNo error, but the selected person can change
selected-at(${adult_list}, ${pick_index} + 1)Use the index as it is; it counts from 0No error, but the first adult is never picked and the last pick is blank
count(${adult_pos}) for the number of adultscount-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:

PlatformWhat to know
ODK Collect and CentralWorks 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 FormbuilderAdd Calculate questions and type the expressions in their calculation boxes; uploading the XLSForm is easier for a pattern with this many rows
SurveyCTOUse 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.

Tip:

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.

Build my first form free

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.