Randomly selecting a subset of possible answer categories
Problem
You want to show a random subset of possible answer categories to the respondent.
Solution
Use the select_random_combination function:
where
Discussion
Sometimes, it is necessary to select a random subset of answer categories to be offered to the respondent. An example would be to randomly select two magazines and to ask the respondent to select the one they prefer. The special function select_random_combination is used for this purpose, along with the display of answer choices using category display conditions.
In the context of a CALCUL question, let's analyze the following call to this function:
This would place, in Q1 (which must have a maximum number of answers greater than 1 to accommodate pairs of selections or trios, etc.), a random selection of two integers between 1 and 5. The random selection will necessarily be different from the selections stored in Q2 and Q3 but it could include an integer already selected in Q2 or Q3.
This would place, in Q1, a random selection of three integers between 1 and 9. The random selection will necessarily be different from the selections stored in Q2 and Q3 and it cannot include an integer already selected in Q2 or Q3.
It is usually preferable to calculate these random selections only once so that, if the respondent backtracks in the questionnaire, he/she doesn't get assigned a different random selection. The following code achieves this:
If it is not possible to find a combination because of the constraints imposed by the programmer, the function returns "---". This would happen:
The result from this function can be used in another question which will display answer categories based on the calculated question. For example:
will display only the choices that were randomly selected in the Q1 subset.