# TOPIC: Showing a random subset of all possible groups

## Showing a random subset of all possible groups 2 years 4 months ago #96625

 DaveyKrebs Offline Fresh Lemon Posts: 10 Karma: 0 Hello, I'm trying to set up a questionnaire that will display only a random subset of all possible groups to participants, and I'm wondering, is it possible to do this with Expression Manager (or a combination of javascript and EM)? Within each group is a number of question variants, only one of which will be displayed to participants, selected at random. I found this to be very easy to implement with EM, thanks to this example. Displaying only a subset of possible groups is more difficult since multiple groups need to qualify as relevant, and the selection of each group needs to be independent of the other groups. That is, each group can't simply occur with a fixed selection of other groups; the probability of occurring with any other group must be equal. I'm currently using an unsatisfactory work-around, in which I have a hidden equation question corresponding to each group that randomly generates 1 or 2, with the relevance of each group set to 1. This means that each group has 50% probability of being displayed to any given participant, and that each participant will see, on average, half of the possible groups, but also that some participants could end up seeing quite a bit more/less than that, which is undesirable. I thought that a possible solution would be to have a hidden question that randomly shuffles an array of integers (corresponding to the total number of groups) and then takes a slice of that shuffled array. Each group would have a unique relevance value corresponding to an integer from within this array, and would be set to display if it's value is among the slice generated. This sounds simple enough to explain, but as I'm nearly programming illiterate, I don't know how difficult it is to actually implement. The first part I could probably manage but I don't if the second part is within the current capabilities of the relevance equations to specify. Is it possible, and if so, how? If you've read this far, many thanks for your patience, please forgive my ignorance, and many apologies if I've accidentally left out any important details. Any reply would be much appreciated. The administrator has disabled public write access.

## Showing a random subset of all possible groups 2 years 4 months ago #96675

 DaveyKrebs Offline Fresh Lemon Posts: 10 Karma: 0 Ok, no-one's replied, so I'm guessing my question was problematic in some way, but it would really help to know what way that was. Please, any feedback at all would be really helpful; e.g. "I don't understand what you're asking", or "That's not possible", or "That is possible but I'm not going to explain how", or "It's really obvious and you should easily figure out the answer", or even "I don't know". Incidentally, I'm not the only one who's asking this type of question. This post by Iosononando is essentially about the same thing. Many thanks! The administrator has disabled public write access.

## Showing a random subset of all possible groups 2 years 4 months ago #96684

 tpartner Offline LimeSurvey Team Posts: 5157 Thank you received: 1066 Karma: 450 Placing this script in the source of a multiple-numeric question will shuffle an array of group numbers and then load the numeric inputs with the corresponding array values. (So, if for example, you only want to capture the first 4 random numbers, use 4 subquestions it the multiple-numeric.) You can then use relevance/conditions to display following groups depending on the multiple-numeric values. `````` Cheers, Tony Partner Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise. LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project. The administrator has disabled public write access. The following user(s) said Thank You: DaveyKrebs

## Showing a random subset of all possible groups 2 years 4 months ago #96700

 DaveyKrebs Offline Fresh Lemon Posts: 10 Karma: 0 Thanks very much for your reply, and for the javascript code - it works perfectly. However, I'm still having trouble using the multiple-numeric values to control group displays. I tried an example with 10 groups, and specified 10 subquestions in the multiple-numeric, then set the relevance of following groups to display if the corresponding subquestion returned a value of less than or equal to 5. The effect should have been to display only 5 random selected groups, but it doesn't work. Instead, they all display, despite the survey logic coming out clear. I tried the way you initially suggested but couldn't figure out to define an appropriate variable (the multiple-numeric question code was reported to be "undefined" but the subquestion codes were fine). I'm attaching a copy of my survey - perhaps someone could take a look and see if it's an easily fixable problem? Many thanks again Tony! Attachments: ...632.lss (54KB) The administrator has disabled public write access.

## Showing a random subset of all possible groups 2 years 4 months ago #96710

• tpartner
• LimeSurvey Team
• Posts: 5157
• Karma: 450

In the attached survey, I've modified your relevance equations to something like:
`rask_SQ01.NAOK gt 0 && rask_SQ01.NAOK le 5`

And, since the survey is in "all-in-one" mode, added a snippet to the script that fires the checkconditions() function after loading the numeric inputs:
```<script type="text/javascript" charset="utf-8">

// Identify the question
var q1ID = {QID};
var q1 = \$('#question'+q1ID+'');
var inputsCount = \$('input[type="text"]', q1).length;

if(\$('input[type="text"]:eq(0)', q1).val() == '') {

// An array holding the number of groups
var groups = [1,2,3,4,5,6,7,8,9,10];

// Shuffle the array
function shuffle(o){
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(groups);

// Load the numeric inputs with the corresponding array values
\$(groups).each(function(i){
if(i < inputsCount) {
var el = \$('input[type="text"]:eq('+i+')', q1);
\$(el).val(this);
checkconditions(\$(el).attr('value'), \$(el).attr('name'), \$(el).attr('type'));
}
});
}

});
</script>```

#### File Attachment:

File Name: limesurvey...2_TP.lss
File Size:55 KB

If you want to use my approach, the relevance equations should look something like (one line per group):
```rask_SQ01.NAOK == 1 OR rask_SQ02.NAOK == 1 OR rask_SQ03.NAOK == 1 OR rask_SQ04.NAOK == 1 OR rask_SQ05.NAOK == 1