# Draw a random number

Hello,

I am trying to draw a random number between 1 and 5 (inclusive). The point would be to make certain questions relevant based on this draw. Any help would be greatly appreciated.

Thanks,

TK

You can do *random()5 in calculation of field: calculate

random() output random number in [0,1]. XFORM does the rounding to integer.

··· On Wednesday, 30 September 2015 20:46:18 UTC+1, Tanu Kumar wrote: > > Hello, > > I am trying to draw a random number between 1 and 5 (inclusive). The point > would be to make certain questions relevant based on this draw. Any help > would be greatly appreciated. > > Thanks, > > TK

I think you are close, Hung.

The traditional way of doing this is: floor(random() * (max - min + 1)) + min.

JavaROSA doesn't have floor, so you can do it like: int(random() *
(max - min + 1)) + min.

Caveat is this only works if you are expecting positive numbers from
random() * (max - min + 1).

Yaw

··· -- Need ODK services? http://nafundi.com provides form design, server setup, professional support, and software development for ODK.

On Fri, Oct 2, 2015 at 5:25 AM, Hung Pham hpham2709@gmail.com wrote:

You can do random()*5 in calculation of field: calculate

random() output random number in [0,1]. XFORM does the rounding to integer.

On Wednesday, 30 September 2015 20:46:18 UTC+1, Tanu Kumar wrote:

Hello,

I am trying to draw a random number between 1 and 5 (inclusive). The point
would be to make certain questions relevant based on this draw. Any help
would be greatly appreciated.

Thanks,

TK

## --

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

Nice. I learn something new today.

Thanks Yaw.

Should start looking into JavaROSA source code soon.

Hung

Hung,

One easy contribution is for you to add floor() and ceil() to the
JavaROSA code. Give it a try!

Yaw

··· -- Need ODK consultants? https://nafundi.com provides form design, server setup, in-field training, and software development for ODK.

On Fri, Oct 2, 2015 at 3:47 PM, Hung Pham hpham2709@gmail.com wrote:

Nice. I learn something new today.

Thanks Yaw.

Should start looking into JavaROSA source code soon.

Hung

## --

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

Yaw,

I will give it a try and report back for result.

Hung.

And whenever you use random(), you should wrap it with once()

··· On Fri, Oct 2, 2015 at 2:17 AM, Hung Pham wrote:

Yaw,

I will give it a try and report back for result.

Hung.

## --

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

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com

Search the list for once( or random( for earlier posts

··· On Tue, Oct 6, 2015 at 5:19 PM, Mitch Sundt wrote:

And whenever you use random(), you should wrap it with once()

On Fri, Oct 2, 2015 at 2:17 AM, Hung Pham hpham2709@gmail.com wrote:

Yaw,

I will give it a try and report back for result.

Hung.

## --

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