Random field selected from roster based on two criteria and then used to %prefill% questions throughout the survey


I have searched far and wide and can’t seem to get the syntax correct for a crop cut survey.

If the target parcel is divided into multiple fields and meet certain criteria from the roster (1. growing the crops of interest 2. not intercropped). We need to randomly select one of these fields and then use the name of that field to populate further questions in the section.

I have tried different methods for randomizing, but the two issues I keep running into are 1. That I am trying to randomize based on two criteria (E4 and E5_culture) and 2. getting the actual name of the chosen field to %populate%.

I would love help on syntax that can randomly select one of the eligible fields from the roster and syntax that will pull the name of that field (as a string variable or otherwise) into the next question.

See attached survey for my current workaround. https://designer.mysurvey.solutions/questionnaire/details/6471cd39-6975-4b31-8a73-cf32cd1be2a3

Hello hhannigan,

I will try to give a try at your questionnaire, but let me see if I can help you by explaining how we have done this in the past in Belize.

Just like you, we have had the need to select a random element from a roster based on specific criteria. In our case, it was a selection of a random household member over the age of 18. This is how we approached it, adapting the syntax/documentation that the Survey Solutions team has previously shared (such as here). Let me illustrate how we achieved this, using the example of a randomly selected person of 18 years or over.

  1. Identify the number of eligible elements, based on your criteria. For us, the criteria was that the age is >= 18. Save this in a variable called num_eligible.
MEMBER_ROSTER.Count(x=>x.AGE >= 18)
  1. Select a random index based on your count at step 1. (Example, if you have 5 eligible elements, select a random number from 1 to 5). Save this in a variable called rnd_index
  1. Get the rowcode (actual position in the roster) that belongs to the element you will randomly select. We again specify the criteria of eligibility when fetching roster items here. Save this in a variable rnd_rowcode.
MEMBER_ROSTER.Where(x=>x.AGE >= 18 ).Select(z=>z.@rowcode).ToArray()[rnd_index.Value]

Afterwards, you will define another roster with the questions you want to ask to the selected element. This roster will have the same source as your roster with all eligibile elements, but you can add an enabling condition to only ask to the randomly selected element based on the @rowcode. For example: @rowcode == rnd_rowcode.

The code described in steps 1-3 could be refactored and collapsed, but I wanted to break it down to illustrate the different steps.

I hope this helps.


@giansib, This works perfectly, but how do I format the syntax for Step 1 in cases of two criteria? For example if your criteria was age >= 18 and gender= 1?

Thank you!

Glad to hear, @hhannigan!
And in that case the condition could be

MEMBER_ROSTER.Count(x=>(x.AGE >= 18 && x.GENDER == 1))

@giansib, This works well for selecting one field to display- but to address an issue later in the survey where we need to select three random rows of trees within the field. (Ex. They have 7 rows of trees, rows 1,4,7 are randomly selected and then open a roster for each). It might not be possible, but is there at least a way to generate 3 numbers with the upper bound based on a numeric question? I have searched the forum but I can’t seem to get any of those situations to apply.

You will find the example of multiple persons selection in the HH in the ‘Public example User questions and common patterns’ questionnaire.

Why not?

@sergiy, I am not pulling from a roster- I simply need to pull from a numeric question: “How many rows of trees are there?” and the enumerator should then be given 3 random numbers based on the response.
Like this case, but three numbers instead of one.

Given some number NumElig1 the example yields two numbers i1 and i2 which are:

a) each between 1 and NumElig1, and
b) i1 not equal to i2.

How is this not suitable for your case? (aside from you wishing for three numbers, not two)?

contradicts what you’ve written earlier:

See “PUBLIC EXAMPLE Lottery” questionnaire.

Bless you, Sergiy! This is exactly what I need. I’m not sure my base level of C# knowledge could have figured this out in time.