Randomly present randomly selected subsets of elements from different groups | XM Community
Skip to main content

Hi!
My question's title seems more complex than my problem really is.
To simply describe my project, I have 8 groups of 15 elements each and want to present 5 elements within in groups in a random order (across groups). I've tried nesting randomizers under a main randomizer, but even though only 5 elements per group are presented and groups are presented in a random order, the 5 elements from a group are presented one after the one (rather than completely randomized across groups).
The basic skeleton of my survey flow looks like this:
Randomizer: "randomly present 8 of 8"
Randomizer for Group 1: "randomly present 5 of 15"
Element 1
Element 2
...
Element 15
Randomizer for Group 2: "randomly present 5 of 15"
...
Randomizer for Group 8: "randomly present 5 of 15"

Please note that my "elements" include whole blocks of questions and branching logic, so using the question randomization within the block option doesn't apply.

Thanks!

I have 1 idea using randomizer elements but I also have solid randomization procedures with JavaScript. If you share a minimally reproducible example of your survey in a .qsf (please do include the branch logic and the question blocks so I can see if there is a way to reduce that as well), I can have a go.


npetrov937, thanks for volunteering to help!
Here is my .qsf file. As you'll be able to see, what currently happens is that blocks appear in that order, for example:
G(roup)2.B(lock)1. / G2.B3. / G1.B2. / G1.B3.
It works as only 2 out of 3 blocks are selected per group BUT the blocks from specific groups are displayed one after the other. I would like it to appear like this, for example:
G2.B1. / G1.B2. / G2.B3. / G1.B3.
Please note that I simplified my survey A LOT. Otherwise, I have 144 blocks. That being said, I appreciate your help, but please don't spend too much time trying to figure a way to do this, especially if it's going to take a lot of copy-pasting of coding from my part. I was just curious to know if there was a way do to the randomization simply. If not, I have another way that satisfy me.
Reprex (1).qsf


There you go - nice and simple (for the most part).
Reprex.qsfI merely use JS in the first Q to randomly select the block (i.e. do the job of one of your randomizers) and then only use a randomizer to randomize the display of the blocks.
The only annoying thing with this setup is to create the IF conditions in the survey flow in the way I've started them - a bit tedious for 144 blocks but should be easily done in 15-ish minutes. Then you need to enter your groups/blocks in the javascript object (called blocks_dictionary) but with a good text editor, that's a 2-minute job at worst.
Hope that helps!


Hello npetrov937,
I tried to use, in my survey, the code kindly made available here. However, for some reason, sometimes it does not work as I expected.
Here is the thing: I have a pool of 100 words (50 from "Group1" and 50 from "Group2"); each word is in a different block. From the pool of 100 words, I would like to randomly select 50 words - and only those would be presented to the participant: 25 from "Group1" and 25 from "Group2"; also presented in a random order.
Using the piece of code above, indeed 50 words are presented; however, sometimes, the survey displays 26 words from "Group1" and 24 from "Group2" (instead of 25 + 25 words).

Could you please help me trying to figure it out what might be going wrong? Thank you!
Here is the piece of code I am using:

Qualtrics.SurveyEngine.addOnload(function()
{
//function to get n random elements from an array
function getRandom(arr, n) {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
}

//defining the groups and the blocks for each group
blocks_dictionary = {
"Group1" : ["a1word", "a2word", "a3word", "a4word", "a5word", "a6word", "a7word", "a8word", "a9word", "a10word", "a11word", "a12word", "a13word", "a14word", "a15word", "a16word", "a17word", "a18word", "a19word", "a20word", "a21word", "a22word", "a23word", "a24word", "a25word", "a26word", "a27word", "a28word", "a29word", "a30word", "a31word", "a32word", "a33word", "a34word", "a35word", "a36word", "a37word", "a38word", "a39word", "a40word", "a41word", "a42word", "a43word", "a44word", "a45word", "a46word", "a47word", "a48word", "a49word", "a50word"],
"Group2" : ["b1word", "b2word", "b3word", "b4word", "b5word", "b6word", "b7word", "b8word", "b9word", "b10word", "b11word", "b12word", "b13word", "b14word", "b15word", "b16word", "b17word", "b18word", "b19word", "b20word", "b21word", "b22word", "b23word", "b24word", "b25word", "b26word", "b27word", "b28word", "b29word", "b30word", "b31word", "b32word", "b33word", "b34word", "b35word", "b36word", "b37word", "b38word", "b39word", "b40word", "b41word", "b42word", "b43word", "b44word", "b45word", "b46word", "b47word", "b48word", "b49word", "b50word"]
}


//creating an array for the current pp by going through each group's blocks and selecting 25 random blocks
current_PP_array = []
for (var group in blocks_dictionary) {
current_PP_array = current_PP_array.concat(getRandom(blocks_dictionary[group], 25))
}

Qualtrics.SurveyEngine.setEmbeddedData( "current_PP_blocks", current_PP_array)
this.clickNextButton()

});


Leave a Reply