Randomize images without replacement, with conditions | XM Community
Skip to main content

Hi There,
I'm have a list of images embedded in javascript, which I randomize without replacement 4 times (same image cannot appear twice) and then save to qualtrics as embedded text. Here is my current javascript:
Qualtrics.SurveyEngine.addOnload(function(){
  function shuffle(array){
  var counter = array.length,
  temp, index;
  while (counter > 0){
  index = Math.floor(Math.random() * counter);
  counter = counter-1;
  temp = array=counter];
  arraycounter] = array index];
  array>index] = temp;
  }
  return array;
  }

var BF1 = {'BF1' : "https://uwmadison.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_6MqvF92gFjIQdvL"};
var BA1 = {'BA1' : "https://uwmadison.co1.qualtrics.com/CP/Graphic.php?IM=IM_2ai3h65t8KDXKAt"};
var BF2 = {'BF2' : "https://uwmadison.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_3lOsHtTOhx9brSd"};
var BA2 = {'BA2': "https://uwmadison.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_6rmHa8ZemfjNYI5"};
var BF3 = {'BF3': "https://uwmadison.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_0IWXF8YYUj7mLbv"};
var BA3 = {'BA3': "https://uwmadison.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_eV4UXXA1KeTmmTX"};
var BA4 = {'BA4': "https://uwmadison.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_1Odotlg2jvIMciV"};
var BF4 = {'BF4': "https://uwmadison.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_9zgpuMgqHh7QQ2F"};

  var myArray= BF1,BA1,BF2, BA2, BF3,BA3, BA4, BF4];
 
  shuffle(myArray);
Qualtrics.SurveyEngine.setEmbeddedData("firstImage",Object.values(myArray.0])u0]);
Qualtrics.SurveyEngine.setEmbeddedData("firstImageName",Object.keys(myArrayc0])e0]);

Qualtrics.SurveyEngine.setEmbeddedData("secondImage",Object.values(myArray.1])u0]);
Qualtrics.SurveyEngine.setEmbeddedData("secondImageName",Object.keys(myArrayc1])e0]);

Qualtrics.SurveyEngine.setEmbeddedData("thirdImage",Object.values(myArray.2])u0]);
Qualtrics.SurveyEngine.setEmbeddedData("thirdImageName",Object.keys(myArrayc2])e0]);

});

I want to add a condition, which suggests that an image name that has the same number cannot be drawn in the next draw.
To demonstrate, my array includes the following vars: BF1, BA1, BF2, BA2, BF3, BA3, BF4, BA4. And I want to draw 3 images with the said condition. Let's say in the first draw I drew BA2. So the possible vars in my next draw will be:
BF1, BA1, BF3, BA3, BF4, BA4 (note that I removed any var with the number 2).
Let's say my next drawn var was BA1, now the possible list of vars is:
BF3, BA3, BF4, BA4 (note that I removed any var with the numbers 2 or 1).

I hope this makes sense, and I'd appreciate any help!

rondev TomG SurajK - you've been super helpful with javascript questions in the past, I was just wondering if you have any idea how to solve this. Thanks in advance!!


Leave a Reply