All instances of "0.5" in the formulae below should be "1". Sorry about that; I've been working sometimes with int() and sometimes with round().
Sam
Hi,
I found this very useful - thank you!
I am trying to adapt Legarraudien's program to select multiple household members at random by generating a number of different random numbers. The problem I have is that I cannot guarantee the same member is not picked twice, since int(0.5+${total_number_in_household}*{random_number})) rounds to the same integer for a range of random numbers. To work around this for selecting two household members, I used the following code for chosen member 2:
if(int(0.5+${total_number_in_household}{random_number2}) != (0.5+${total_number_in_household}{random_number1}), int(0.5+${total_number_in_household}{random_number2}),if(int(0.5+${total_number_in_household}{random_number2})=${total_number_in_household},1,int(0.5+${total_number_in_household}*{random_number2})+1))
In other words, if the second selected member is the same as the first selected member, pick the next person on the list unless the selected person is the last person on the list, in which case pick the first. This seems to work, but it is very cumbersome and will get a lot worse when I want to select 3 or more household members.
The other idea I had was to assign each household member a random number and then only show a household member if the random number corresponding to that member is greater than a certain threshold value (following this thread: https://groups.google.com/forum/#!msg/formhub-users/ndcyBG3I-og/2IiO0DlDYaEJ). But if I did that, I would not be able to ensure that the "right" number of household members is selected (i.e. if I want to select 4 household members, I will sometimes get 3, sometimes 5 etc). Unlike the OP on that thread, I do not know in advance the number of members on the roster so I can't generate the members to select in advance.
Is there another possibility? For example, could I assign each household member a random number, then pick the highest, second highest up to kth highest random numbers (where k is my sample size). That's what I would do in Stata - but I can't figure out a way of doing it here!
I didn't attach a mock xlsform since my problem is so related to the earlier one in the thread but I could attach one if that would be helpful.
Thanks a lot,
Sam
The once() should contain just the random number.
I.e., create a field "random_pick" calculate = "once(random())"
Then have the computed expression, "chosen_member":
if(count-selected(/data/membersinrange) = 0, 0,
int(1.0+count-selected(/data/membersinrange)*/data/random_pick))
The problem is that after the first time through the loop, javarosa detects that /data/membersinrange has changed, which triggers the evaluation of chosen_member, which assigns 1 to chosen_member.
The second time through the loop, you add a 2nd member, javarosa detects that /data/membersinrange has changed, and triggers a re-evaluation of chosen_member, but because of the once() function, the re-computation is prevented and remains 1.
This does mean that if you edit the form and delete or add more members, you will end up with a different chosen member. Only if the number of members is unchanged, will the chosen_member value also be unchanged.
Ok, I didn't know that. I wait for a response
Hi Pierre,
Please note that Mitch (and really everyone else on this list) provides free support in their spare time.
If you can't wait for a response, then I encourage you to hire one of the many ODK companies available at http://opendatakit.org/help/help-for-hire/
Thanks,
Yaw
--
Need ODK services? http://nafundi.com provides form design, server setup, professional support, and software development for ODK.
HI MITCH,
Please can you tell me what wrong with my program? Sure,i forget something important, byt idon't identify it.
thanks a lot
Hi,
I have to select randomly a member in a household survey. fisrt of all, I Ask for basic demographics in a repeat
group.
begin
repeat
member
roster
calculate
CR360
Member ID number
text
CR361
Member's Name
select_one
gender
CR371
Member's gender
integer
CR381
Member's age
date
CR391
Date of birth
end repeat
member
Then have a series of calculated field
that produce a space-separated list of the indices into that repeat group that
are within the designated age range.
calculate
ageok1
if(/data/member[1]/CR381 >= 5
and /data/member[1]/CR381 <= 15, ' 1', '')
calculate
ageok2
if(/data/member[2]/CR381 >= 5 and /data/member[2]/CR381
<= 15, ' 2', '')
calculate
ageok3
if(/data/member[3]/CR381 >= 5 and /data/member[3]/CR381
<= 15, ' 3', '')
calculate
ageok4
if(/data/member[4]/CR381 >= 5 and /data/member[4]/CR381
<= 15, ' 4', '')
calculate
ageok5
if(/data/member[5]/CR381 >= 5 and /data/member[5]/CR381
<= 15, ' 5', '')
calculate
membersinrange
substr(concat(${ageok1},${ageok2},${ageok3},${ageok4},${ageok5}),1)
And this function once to compute the chosen member once. But in my program , the note1 display nothing and the member chosen is 0. Can somebody help me?
note
note1
${membersinrange}
calculate
chosenmember
once(if(count-selected(/data/membersinrange) = 0, 0,
int(1.0+count-selected(/data/membersinrange)*random())))
note
Child
child's name is
${chosenmember}
--
--
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: 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: 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: 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.
--
Mitch Sundt
Software Engineer
University of Washington
mitche...@gmail.com
I can't see how it was resolved from the thread.