# Random number without duplicates/without replacement

**URL:** <https://forum.getodk.org/t/random-number-without-duplicates-without-replacement/6133>\
**Category:** Support\
**Created:** [December 7, 2016, 2:00pm UTC](https://forum.getodk.org/t/random-number-without-duplicates-without-replacement/6133 "2016-12-07T14:00:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bouguen\_Adrien](https://getodk.b-cdn.net/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@Bouguen\_Adrien](https://forum.getodk.org/u/Bouguen_Adrien)\
**Post date:** [December 7, 2016, 2:00pm UTC](https://forum.getodk.org/t/random-number-without-duplicates-without-replacement/6133/1 "2016-12-07T14:00:09Z")

</div>

Dear all,

I need to sample 20 households based on a household list. I was thinking I  
could sample them using ODK and the household rank number. Hence, I was  
looking for a way to generate 20 random numbers on ODK. I did the following  
1+int(once(random())\*${total number of households in the village}) but this  
give me a random number with replacement and hence possibly with  
duplicates. I am looking to a way to do the same without replacement. Any  
idea?

Thanks

---

<div class="post-metadata">

**Author:** ![chrislrobert](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/chrislrobert/32/1613_2.png) [@chrislrobert](https://forum.getodk.org/u/chrislrobert)\
**Post date:** [December 7, 2016, 2:18pm UTC](https://forum.getodk.org/t/random-number-without-duplicates-without-replacement/6133/2 "2016-12-07T14:18:28Z")

</div>

It's a bit tricky, but can be done. Attached is a SurveyCTO sample form  
that demonstrates all of the mechanics. I don't believe that it uses any  
SurveyCTO-specific functions, but please note that it's very sensitive to  
the dynamics of repeat-group creation and calculation evaluation; it could  
be that SurveyCTO's JavaRosa engine differs enough from ODK's that this  
form doesn't work well on ODK Collect. So please test it thoroughly before  
relying on it.

Best,

Chris

P.S. Sorry, looking at it now, I see that it does use "index()", which can  
be changed to "position(..)" for ODK. But then it also uses the  
de-duplicate() function, and I don't know that ODK has that. If not, we're  
happy to contribute it. Perhaps you'll be inspired by the approach, though,  
and you can find a pure-ODK way to accomplish the same goal. The trick is  
to keep drawing as many draws as you need to get x unique ones. It is a  
common need for SurveyCTO users, and probably for many ODK users more  
broadly. So would be great to have a working example here in this forum..!

[Sample Form - Random draws without duplicates.xlsx](https://getodk.b-cdn.net/uploads/default/original/1X/14de3f0a8ce91f636cffdf33af744feb9dfbae16.xlsx) (69.5 KB)

> **···**
>
> --- Christopher Robert Dobility, Inc. (SurveyCTO) http://www.surveycto.com/ http://blog.surveycto.com/
> 
> On Wed, Dec 7, 2016 at 9:00 AM, Bouguen Adrien [adbouguen@gmail.com](mailto:adbouguen@gmail.com) wrote:
> 
> > Dear all,
> > 
> > I need to sample 20 households based on a household list. I was thinking I  
> > could sample them using ODK and the household rank number. Hence, I was  
> > looking for a way to generate 20 random numbers on ODK. I did the following  
> > 1+int(once(random())\*${total number of households in the village}) but this  
> > give me a random number with replacement and hence possibly with  
> > duplicates. I am looking to a way to do the same without replacement. Any  
> > idea?
> > 
> > Thanks
> > 
> > ## --
> > 
> > Post: [opendatakit@googlegroups.com](mailto:opendatakit@googlegroups.com)  
> > Unsubscribe: [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com)  
> > Options: [http://groups.google.com/group/opendatakit?hl=en](http://groups.google.com/group/opendatakit?hl=en)
> > 
> > * * *
> > 
> > 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  
> > email to [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Mydogpearls](https://getodk.b-cdn.net/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@Mydogpearls](https://forum.getodk.org/u/Mydogpearls)\
**Post date:** [December 7, 2016, 7:18pm UTC](https://forum.getodk.org/t/random-number-without-duplicates-without-replacement/6133/3 "2016-12-07T19:18:29Z")

</div>

I tried the example provided by Chris Robert and, as suspected, Collect (1.4.12) did not like the function 'de-duplicate()'. This would be a great contribution as I've run into this problem as well.

Best,

Chris Corey

---

<div class="post-metadata">

**Author:** ![chrislrobert](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/chrislrobert/32/1613_2.png) [@chrislrobert](https://forum.getodk.org/u/chrislrobert)\
**Post date:** [December 9, 2016, 12:32pm UTC](https://forum.getodk.org/t/random-number-without-duplicates-without-replacement/6133/4 "2016-12-09T12:32:32Z")

</div>

We're working on contributing that and other extended functions just now.  
Stay tuned. And sorry that it didn't work out of the box. I hope that the  
technique might still inspire a related solution that doesn't require  
de-duplicate for now.

Best,

Chris

> **···**
>
> --- Christopher Robert Dobility, Inc. (SurveyCTO) http://www.surveycto.com/ http://blog.surveycto.com/sneak-peek-surveycto-2-20- supercharges-data-visibility/
> 
> On Wed, Dec 7, 2016 at 2:18 PM, [mydogpearls@gmail.com](mailto:mydogpearls@gmail.com) wrote:
> 
> > I tried the example provided by Chris Robert and, as suspected, Collect  
> > (1.4.12) did not like the function 'de-duplicate()'. This would be a great  
> > contribution as I've run into this problem as well.
> > 
> > Best,
> > 
> > Chris Corey
> > 
> > ## --
> > 
> > Post: [opendatakit@googlegroups.com](mailto:opendatakit@googlegroups.com)  
> > Unsubscribe: [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com)  
> > Options: [http://groups.google.com/group/opendatakit?hl=en](http://groups.google.com/group/opendatakit?hl=en)
> > 
> > * * *
> > 
> > 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  
> > email to [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![yanokwa](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/yanokwa/32/1695_2.png) [@yanokwa](https://forum.getodk.org/u/yanokwa)\
**Post date:** [December 9, 2016, 12:55pm UTC](https://forum.getodk.org/t/random-number-without-duplicates-without-replacement/6133/5 "2016-12-09T12:55:34Z")

</div>

And for those who want to track progress on this issue, it's been filed at  
[https://github.com/opendatakit/collect/issues/292](https://github.com/opendatakit/collect/issues/292).

Yaw

> **···**
>
> On Fri, Dec 9, 2016 at 12:32 PM, Christopher Robert wrote:
> 
> > We're working on contributing that and other extended functions just now.  
> > Stay tuned. And sorry that it didn't work out of the box. I hope that the  
> > technique might still inspire a related solution that doesn't require  
> > de-duplicate for now.
> > 
> > Best,
> > 
> > Chris
> > 
> > * * *
> > 
> > Christopher Robert  
> > Dobility, Inc. (SurveyCTO)  
> > [http://www.surveycto.com/](http://www.surveycto.com/)  
> > [http://blog.surveycto.com/sneak-peek-surveycto-2-20-supercha](http://blog.surveycto.com/sneak-peek-surveycto-2-20-supercha)  
> > rges-data-visibility/
> > 
> > On Wed, Dec 7, 2016 at 2:18 PM, [mydogpearls@gmail.com](mailto:mydogpearls@gmail.com) wrote:
> > 
> > > I tried the example provided by Chris Robert and, as suspected, Collect  
> > > (1.4.12) did not like the function 'de-duplicate()'. This would be a great  
> > > contribution as I've run into this problem as well.
> > > 
> > > Best,
> > > 
> > > Chris Corey
> > > 
> > > ## --
> > > 
> > > Post: [opendatakit@googlegroups.com](mailto:opendatakit@googlegroups.com)  
> > > Unsubscribe: [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com)  
> > > Options: [http://groups.google.com/group/opendatakit?hl=en](http://groups.google.com/group/opendatakit?hl=en)
> > > 
> > > * * *
> > > 
> > > 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  
> > > email to [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > ## --
> > 
> > Post: [opendatakit@googlegroups.com](mailto:opendatakit@googlegroups.com)  
> > Unsubscribe: [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com)  
> > Options: [http://groups.google.com/group/opendatakit?hl=en](http://groups.google.com/group/opendatakit?hl=en)
> > 
> > * * *
> > 
> > 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  
> > email to [opendatakit+unsubscribe@googlegroups.com](mailto:opendatakit+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
