# Select a random number/kishGrid

**URL:** <https://forum.getodk.org/t/select-a-random-number-kishgrid/6331>\
**Category:** Support\
**Created:** [February 28, 2017, 2:35pm UTC](https://forum.getodk.org/t/select-a-random-number-kishgrid/6331 "2017-02-28T14:35:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Helmi](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ali_helmi/32/1666_2.png) [@Ali\_Helmi](https://forum.getodk.org/u/Ali_Helmi)\
**Post date:** [February 28, 2017, 2:35pm UTC](https://forum.getodk.org/t/select-a-random-number-kishgrid/6331/1 "2017-02-28T14:35:39Z")

</div>

_Hello everyone,_

I am new to ODK. Could you please help me to know how to select a random  
number out of a total number of the HH members?

In other words, if the total number of the HH members is 7, we want to  
select a random number/member out of the 7 members (from 1 to 7), how can  
we implement this in XLSForm?

Looking forward to hearing from you.

All the Best,

Ali Helmi

---

<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:** [March 9, 2017, 12:39am UTC](https://forum.getodk.org/t/select-a-random-number-kishgrid/6331/2 "2017-03-09T00:39:09Z")

</div>

Hi Ali,

See [https://groups.google.com/d/msg/opendatakit/t-TF7v\_LMFs/C3AxEpl9BwAJ](https://groups.google.com/d/msg/opendatakit/t-TF7v_LMFs/C3AxEpl9BwAJ)

The general approach would be to have a calculated field such as:

once(int(7\*random())+1)

This would generate a random number between 1 and 7, inclusive.

Yaw

> **···**
>
> On Tue, Feb 28, 2017 at 9:35 AM, Ali Helmi wrote: \> Hello everyone, \> \> I am new to ODK. Could you please help me to know how to select a random \> number out of a total number of the HH members? \> \> In other words, if the total number of the HH members is 7, we want to \> select a random number/member out of the 7 members (from 1 to 7), how can we \> implement this in XLSForm? \> \> Looking forward to hearing from you. \> \> All the Best, \> \> Ali Helmi \> \> -- \> -- \> Post: opendatakit@googlegroups.com \> Unsubscribe: opendatakit+unsubscribe@googlegroups.com \> Options: 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. \> For more options, visit https://groups.google.com/d/optout.

---

<div class="post-metadata">

**Author:** ![Ali\_Helmi](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ali_helmi/32/1666_2.png) [@Ali\_Helmi](https://forum.getodk.org/u/Ali_Helmi)\
**Post date:** [March 9, 2017, 9:05am UTC](https://forum.getodk.org/t/select-a-random-number-kishgrid/6331/3 "2017-03-09T09:05:57Z")

</div>

Hi Yaw,

Thank you so much for your help on this.

I have replaced the number 7 in the code

once(int(7\*random())+1)

to be the variable name of the total number of members in the HH

_once(int(${hh\_num}random())+1)  
My question now is, what happen if we delete_ (+1) _at the end of the code?  
and what does (_+1\*) do?

All My Best Regards,

_Ali_

> **···**
>
> On Thursday, March 9, 2017 at 2:39:54 AM UTC+2, Yaw Anokwa wrote: \> \> Hi Ali, \> \> See https://groups.google.com/d/msg/opendatakit/t-TF7v\_LMFs/C3AxEpl9BwAJ \> \> The general approach would be to have a calculated field such as: \> \> once(int(7\*random())+1) \> \> This would generate a random number between 1 and 7, inclusive. \> \> Yaw \> \> On Tue, Feb 28, 2017 at 9:35 AM, Ali Helmi \<ahel...@gmail.com \> wrote: \> \> Hello everyone, \> \> \> \> I am new to ODK. Could you please help me to know how to select a random \> \> number out of a total number of the HH members? \> \> \> \> In other words, if the total number of the HH members is 7, we want to \> \> select a random number/member out of the 7 members (from 1 to 7), how \> can we \> \> implement this in XLSForm? \> \> \> \> Looking forward to hearing from you. \> \> \> \> All the Best, \> \> \> \> Ali Helmi \> \> \> \> -- \> \> -- \> \> Post: opend...@googlegroups.com \> \> Unsubscribe: opendatakit...@googlegroups.com \> \> Options: 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...@googlegroups.com . \> \> For more options, visit 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:** [March 10, 2017, 2:54pm UTC](https://forum.getodk.org/t/select-a-random-number-kishgrid/6331/4 "2017-03-10T14:54:12Z")

</div>

Ali,

See [https://opendatakit.github.io/xforms-spec](https://opendatakit.github.io/xforms-spec) for the definition of  
random(). It says "Returns a random number between 0.0 (inclusive) and  
1.0 (exclusive)."

So, the +1 moves the range of possible numbers up 1 so you don't get  
zero. Try removing it and try it out!

Thanks,

Yaw

> **···**
>
> On Thu, Mar 9, 2017 at 4:05 AM, Ali Helmi wrote: \> \> Hi Yaw, \> \> Thank you so much for your help on this. \> \> I have replaced the number 7 in the code \> \> once(int(7\*random())+1) \> \> to be the variable name of the total number of members in the HH \> \> once(int(${hh\_num}\*random())+1) \> \> \> My question now is, what happen if we delete (+1) at the end of the code? and what does (+1) do? \> \> \> All My Best Regards, \> \> Ali \> \> \> On Thursday, March 9, 2017 at 2:39:54 AM UTC+2, Yaw Anokwa wrote: \>\> \>\> Hi Ali, \>\> \>\> See https://groups.google.com/d/msg/opendatakit/t-TF7v\_LMFs/C3AxEpl9BwAJ \>\> \>\> The general approach would be to have a calculated field such as: \>\> \>\> once(int(7\*random())+1) \>\> \>\> This would generate a random number between 1 and 7, inclusive. \>\> \>\> Yaw \>\> \>\> On Tue, Feb 28, 2017 at 9:35 AM, Ali Helmi wrote: \>\> \> Hello everyone, \>\> \> \>\> \> I am new to ODK. Could you please help me to know how to select a random \>\> \> number out of a total number of the HH members? \>\> \> \>\> \> In other words, if the total number of the HH members is 7, we want to \>\> \> select a random number/member out of the 7 members (from 1 to 7), how can we \>\> \> implement this in XLSForm? \>\> \> \>\> \> Looking forward to hearing from you. \>\> \> \>\> \> All the Best, \>\> \> \>\> \> Ali Helmi \>\> \> \>\> \> -- \>\> \> -- \>\> \> Post: opend...@googlegroups.com \>\> \> Unsubscribe: opendatakit...@googlegroups.com \>\> \> Options: 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...@googlegroups.com. \>\> \> For more options, visit https://groups.google.com/d/optout. \> \> -- \> -- \> Post: opendatakit@googlegroups.com \> Unsubscribe: opendatakit+unsubscribe@googlegroups.com \> Options: 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. \> For more options, visit https://groups.google.com/d/optout.
