# randomly present subquestions from 2 item pools

3 years 1 day ago #161244 by rvenkat
I have to randomly present a subset of items from 2 sets. Set A has 5 items (option A1, option A2, option A3, option A4 and option A5). Set B has 3 items (option B1, option B2 and option B3).

I want to create an array with 5 subquestions such that there are 3 randomly selected items from Set A and 2 randomly selected items from Set B.

The subquestions in Set A and B are not from a previous question.

I have gone through the randomization workarounds, but I can't figure out how to do this with two sets of items.
Any help would be greatly appreciated.

thanks,

rvenkat
The topic has been locked.
LimeSurvey Partners
2 years 11 months ago #161294 by Joffm
Hello, rvenkat,
This is the way I would do it without waiting for a different solution.
Use equations to define the displayed values.

1. randomly extract three values from a string "12345" and join them to a new string strA. (e.g. "134")
There are only 5 over 3 ways to do:
123,124,125,134,135,145,234,235,245,345.
So it is sufficient to create a random number from 1 to 10
2. randomly extract two values from a string "678" and join them to a new string strB. (e.g. "78")
Here are only three variations, 67,68,78.
So it is sufficient to create a random number from 1 to 3

3. Join the two strings -> "13478"

Now you can use the subquestion relevance
Subquestion 1, if "strpos(myString,"1")!== false"
Subquestion 2, if "strpos(myString,"2")!== false"
Subquestion 3, if "strpos(myString,"3")!== false"
...

https://manual.limesurvey.org/Expression_Manager#Implemented_Functions

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: rvenkat
The topic has been locked.
2 years 11 months ago #161345 by rvenkat
Hi Joffm,
Thanks for the solution. I will try it.
I have another solution that someone developed for me.
I will post it in my next messge.

rvenkat
The topic has been locked.
2 years 11 months ago #161346 by rvenkat
This solution will hide 2 subquestions from the first 5 and hide 1 subquestion from the next 3 [there are a total of 8 subquestions - 5 in Set A and 3 in Set B]. It will then randomly display the remaining 5 subquestions [3 from Set A and 2 from Set B]. You can change the number for subquestions in Set A and B, and also how many you want hidden/displayed from each set by making appropriate changes to the script below. One thing to note: subquestions must be numbered as 1, 2, 3 etc., and not as SQ001, SQ002 (which is the Limesurvey default). If you want to use SQ001 format, you'll have to make appropriate changes to the script. This has been tested on all types of arrays, including Array (Text).

rvenkat

```<script type="text/javascript" charset="utf-8">

// Start code Option 1 to 5
var uniqueRandoms = [];
var numRandoms = 5;
function makeUniqueRandom() {
// refill the array if needed
if (!uniqueRandoms.length) {
for (var i = 0; i <numRandoms; i++) {
uniqueRandoms.push(i);
}
}
var index = Math.floor(Math.random() * uniqueRandoms.length);
var val = uniqueRandoms[index];
// now remove that value from the array
uniqueRandoms.splice(index, 1);
return val;
}
// End Option 1 to 5

// Start code Option 6 to 8
var uniqueRandoms2 = [];
var numRandoms2 = 8;
function makeUniqueRandom2() {
// refill the array if needed
if (!uniqueRandoms2.length) {
for (var i = 6; i <numRandoms2; i++) {
uniqueRandoms2.push(i);
}
}
var index = Math.floor(Math.random() * uniqueRandoms2.length);
var val = uniqueRandoms2[index];
// now remove that value from the array
uniqueRandoms2.splice(index, 1);
return val;
}
// End Option 6 to 8

// Option 1 to 5
for (var i = 0; i < 2; i++) {
var rand = 1+makeUniqueRandom();
var rand = ''+rand+'';
\$('#javatbd{SGQ}'+rand+'').hide();
}

// Option 6 to 8
for (var i = 0; i < 1; i++) {
var rand = 1+makeUniqueRandom2();
var rand = ''+rand+'';
\$('#javatbd{SGQ}'+rand+'').hide();
}

});
</script>
```
The topic has been locked.
2 years 11 months ago #161350 by jelo

rvenkat wrote: This has been tested on all types of arrays, including Array (Text).

You might add the version/build of LimeSurvey to prevent people applying your workaround to unsuitable LimeSurvey installations.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
2 years 11 months ago #161370 by rvenkat
The solution I described has been tested in Version 2.65.0+170502.
The topic has been locked.