# Randomly select a name from age group (under 5, 5-15, over 15 )

**URL:** <https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696>\
**Category:** Support\
**Tags:** odk-xforms\
**Created:** [July 1, 2019, 8:14pm UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696 "2019-07-01T20:14:27Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![godlove](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/godlove/32/25747_2.png) [@godlove](https://forum.getodk.org/u/godlove)\
**Post date:** [July 1, 2019, 8:14pm UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/1 "2019-07-01T20:14:27Z")

</div>

Hello Forum

Please, I need help, I want to interview members of the household, they will first give their I will list their names and age. Later, I want my ODK form to randomly select a single person (if available) for each age category (under 5, 5-15 and over 15) and display his/her name for further questions. Consider the example below:

A household is occupied by 8 members of different ages. after listing their names and age, now I want my ODK form to select randomly a single member per each age group if they are available on the interviewed list 0-4, 5-15, or over 15 age categories. A total of three people or less who are going to answer further questions by "repeating groups" see the attached [Register.xls](https://forum.getodk.org/uploads/short-url/5PwxrTpX8rArb55AG3xSeYLnU28.xls) (26.5 KB)

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [July 1, 2019, 8:33pm UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/2 "2019-07-01T20:33:35Z")

</div>

Hi @godlove

unfortunately it's not possible to do such complicated operations in ODK Collect.

---

<div class="post-metadata">

**Author:** ![godlove](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/godlove/32/25747_2.png) [@godlove](https://forum.getodk.org/u/godlove)\
**Post date:** [July 1, 2019, 8:44pm UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/3 "2019-07-01T20:44:53Z")

</div>

Hi @Grzesiek2010  
Thank you for your feedback

---

<div class="post-metadata">

**Author:** ![LN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ln/32/1605_2.png) [@LN](https://forum.getodk.org/u/LN)\
**Post date:** [July 1, 2019, 11:50pm UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/4 "2019-07-01T23:50:07Z")

</div>

You can absolutely do this kind of thing with the ODK form specification! Unfortunately, XLSForm doesn't make that super obvious but you can write [XPath expressions](https://en.wikipedia.org/wiki/XPath) within an XLSForm.

[This form](https://docs.google.com/spreadsheets/d/1tqqEIdBKU-qk-3flgidqWun17ntSoTZYdSxdX7J5HcA/edit#gid=0) shows an example that you can hopefully follow to get to your desired behavior. I used XPath syntax where necessary for filtering and XLSForm's compact `${}` syntax elsewhere. I hope it's clear enough that you can pattern match without necessarily understanding all the details.

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [July 2, 2019, 1:17am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/5 "2019-07-02T01:17:00Z")

</div>

Bugger, @LN bet me to it... but here's my take! 🙂

[categories.xls](https://forum.getodk.org/uploads/short-url/1MQenjEG3mutuYCmQ4NHwDI4hDE.xls) (9.5 KB)

> [@Grzesiek2010](#):
>
> unfortunately it's not possible to do such complicated operations in ODK Collect.

Ha, never under-estimate what cleverness you can put into an XForm, with a bit of creativity... 😉

[alas, I must rescind your 'solution' @Grzesiek2010 and give it to a well-deserving @LN, but I'm sure you'll earn it back soon enough 😁]

---

<div class="post-metadata">

**Author:** ![LN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ln/32/1605_2.png) [@LN](https://forum.getodk.org/u/LN)\
**Post date:** [July 2, 2019, 3:38am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/6 "2019-07-02T03:38:05Z")

</div>

That's great, @Xiphware! I decided not to go in that direction because of the space limitation but I think it may be easier to understand. 👍

---

<div class="post-metadata">

**Author:** ![ARIF\_AZAD\_KHAN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/arif_azad_khan/32/2921_2.png) [@ARIF\_AZAD\_KHAN](https://forum.getodk.org/u/ARIF_AZAD_KHAN)\
**Post date:** [July 2, 2019, 6:24am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/7 "2019-07-02T06:24:42Z")

</div>

Great uses of selected-at function. Thanks for your solution.

---

<div class="post-metadata">

**Author:** ![godlove](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/godlove/32/25747_2.png) [@godlove](https://forum.getodk.org/u/godlove)\
**Post date:** [July 2, 2019, 8:37am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/8 "2019-07-02T08:37:17Z")

</div>

Dear @ [Xiphware](https://forum.getodk.org/u/xiphware)

Thank you for the strong support

Is it possible to count the total number of people who are selected to answer the questions

Best regards

Godlove

---

<div class="post-metadata">

**Author:** ![godlove](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/godlove/32/25747_2.png) [@godlove](https://forum.getodk.org/u/godlove)\
**Post date:** [July 2, 2019, 8:59am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/9 "2019-07-02T08:59:01Z")

</div>

Thank you for the strong support

Is it possible to count the total number of people who are selected to answer the questions

Best regards

Godlove

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [July 2, 2019, 9:32am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/10 "2019-07-02T09:32:08Z")

</div>

Yes, you can use the [count()](https://docs.opendatakit.org/form-operators-functions/#count) function which will give you the number of repetitions, ie number of family members.

See [https://docs.opendatakit.org/form-repeats/#counting-the-total-number-of-iterations](https://docs.opendatakit.org/form-repeats/#counting-the-total-number-of-iterations) for details.

---

<div class="post-metadata">

**Author:** ![godlove](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/godlove/32/25747_2.png) [@godlove](https://forum.getodk.org/u/godlove)\
**Post date:** [July 2, 2019, 9:33am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/11 "2019-07-02T09:33:23Z")

</div>

Can you help me to put in this xls as you desingned[categories.xls](https://forum.getodk.org/uploads/short-url/6h2yzdcvP0FPkyfsJcXk5g7UrYb.xls) (27 KB)

---

<div class="post-metadata">

**Author:** ![godlove](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/godlove/32/25747_2.png) [@godlove](https://forum.getodk.org/u/godlove)\
**Post date:** [July 2, 2019, 10:40am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/12 "2019-07-02T10:40:52Z")

</div>

Sorry for taking you time my friend!.

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [July 2, 2019, 1:07pm UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/13 "2019-07-02T13:07:17Z")

</div>

> [@Xiphware](#):
>
> Ha, never under-estimate what cleverness you can put into an XForm, with a bit of creativity...

Yes sorry, I shouldn't have broken my rule which is `answer if you know a solution and wait for other users if you just want to say sorry it's not possible.`

---

<div class="post-metadata">

**Author:** ![Aizik](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/aizik/32/12766_2.png) [@Aizik](https://forum.getodk.org/u/Aizik)\
**Post date:** [August 25, 2020, 2:48am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/14 "2020-08-25T02:48:51Z")

</div>

If the selected respondent has more than one name, how do I make it pick all the names. For example I have a selected reposndent whose name is Randy Shaw. However is giving me only Shaw instead of Randy Shaw. How do I sort this?  
Thanks

---

<div class="post-metadata">

**Author:** ![Aizik](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/aizik/32/12766_2.png) [@Aizik](https://forum.getodk.org/u/Aizik)\
**Post date:** [August 25, 2020, 2:51am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/15 "2020-08-25T02:51:41Z")

</div>

Xiphware, you very right. I had told someone that over 90% of what you think in an Xform is possible.  
Struggled with a code and at the got it to work by just replacing or with and.

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [August 25, 2020, 6:03am UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/16 "2020-08-25T06:03:02Z")

</div>

> [@Aizik](#):
>
> For example I have a selected reposndent whose name is Randy Shaw. However is giving me only Shaw instead of Randy Shaw. How do I sort this?

Could you attach your form?

---

<div class="post-metadata">

**Author:** ![Olimpia](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/olimpia/32/13363_2.png) [@Olimpia](https://forum.getodk.org/u/Olimpia)\
**Post date:** [February 18, 2021, 5:50pm UTC](https://forum.getodk.org/t/randomly-select-a-name-from-age-group-under-5-5-15-over-15/20696/17 "2021-02-18T17:50:31Z")

</div>

Hello everyone,

I have a similar problem to the one discussed here and wanted to get some help to see what I am missing.

How can I make ODK randomly select 40 numbers without replacement between 1 and N, where N is an integer inputted by the user in a previous question.

More specifically, in question 1, we ask for the number of households in the village. Following this question, I would like insert a 'calculation question' that will randomly select 40 numbers without replacement between 1 to N, where N is the total number of households.

Let me know if you have a nice way of doing this.

Thank you in advance for your help and stay safe

Olimpia
