the:behavioral:lab

How to randomize or shuffle an array in Qualtrics

Qualtrics does many things right. However, its vast capabilities sometimes makes me think it can do things that it can’t. Unlike SurveyMonkey where I just assume it can’t do anything, sometimes I think Qualtrics can do everything. Luckily, Qualtrics’ JavaScript capabilities makes it so if you know some coding, you can do a lot of the things you thought were impossible.

Randomizing arrays of numbers is something Qualtrics can’t do (easily) without JavaScript. Technically, you can create a randomizer, inside the randomizer create X branch elements, set each element to automatically occur (previously set an embedded data element named a, set the value to 1, then set the branches to occur if a=1), create an embdedded data element in each branch, and set the randomizer to randomly show X elements evenly. Pretty difficult, and even that won’t do everything. You would need to add in however you implement the random number or use some piped text code to add that number to a different element to you can end up with a full array after the randomizer ends. Regardless it is difficult.

In Javascript the code is this (here it is on Github):

1:  function shuffleArray(array) {  
2:    for (var i = array.length - 1; i > 0; i--) {  
3:      var j = Math.floor(Math.random() * (i + 1));  
4:      var temp = array[i];  
5:      array[i] = array[j];  
6:      array[j] = temp;  
7:    }  
8:    return array;  
9:  }  

In a question in Qualtrics, you simply insert this code into the javascript editor. One way or another create your array (type it in, grab it from the question text, etc…. if you need help doing this let me know in the comments), shuffle it using this code, then one way or another use it. You can set the array to an embedded data element using the SurveyEngine.setEmbeddedData() function that is already available in the Quatlrics API (mentioned here). You can add it as text to a question (I think that function is mentioned in the previous link, if not its another SurveyEngine function). Again, the implementation options are infinite. If you’ve gotten this far, and don’t know how to use your randomized array, again, let me know in the comments.

Advertisements

Single Post Navigation

7 thoughts on “How to randomize or shuffle an array in Qualtrics

  1. Hi Andrew,

    Not sure if you could help me with this, but I’m essentially trying to do this but with an n-choose-k randomization. I want to select 10 photos from a database of 100 titled images and associated URLs. I then want to present these photos as part of a question text, which I can get Qualtrics to do if I set an embedded variable to equal the URL of that photo and then pipe in the embedded variable.

    I’m stumped as to how to get it to sample 10 from the 100 without replacement, and then display all 10 in random order. Any ideas? Javascripting similar to what you’re doing here seems like the way to go (though this would be incredibly easy if I could just throw in some R script…)

    Thanks!

    • There are three ways. I use a web service that I programmed in PHP. I think it’s easier, but you need to know some basic PHP to use it and you need a server to host it. The third way listed below uses only Qualtrics, but is ridiculously complicated. You mentioned you were OK with Javascript, so I put that way first. Its the same thing I mentioned in the article, but I include how to create the embedded data elements more explicitly.

      1) In a question before you want to show the images, open up the JS editor. Replace everything with what is below.
      Qualtrics.SurveyEngine.addOnload(function()
      {
      function shuffle(array) {
      var counter = array.length, temp, index;
      while (counter > 0) {
      index = Math.floor(Math.random() * counter);
      counter–;
      temp = array[counter];
      array[counter] = array[index];
      array[index] = temp;
      }
      return array;
      }
      var myArray=[“test1″,”test2″,”test3″,”test4”];
      shuffle(myArray);
      Qualtrics.SurveyEngine.setEmbeddedData(“first”,myArray[0]);
      Qualtrics.SurveyEngine.setEmbeddedData(“second”,myArray[1]);
      Qualtrics.SurveyEngine.setEmbeddedData(“third”,myArray[2]);
      });

      Edit the myArray variable so that it contains a comma-delimited, quotation-enclosed list of all 100 URLs. Copy the Qualtrics.SurveyEngine… line until you have the desired number of URLs selected. If you want 10, create 10 of them. The first argument can be whatever text you want. The second argument can also be whatever array element you want, but going from 0-9 makes the most sense to me.

      2) Edit the PHP below such that there is one variable with an image URL assigned to each of 100 variables (should start with $s1=”FIRST URL”; $s2=”SECOND URL”; etc.). List all those variables in the array (right now is $s1 through $s4; it should go all the way to $s100). If you do not have PHP version 5.4 or greater installed, remove the comment from the first echo line, and add 96 more variables to the string that gets echoed. You can name the variable anything you want (i.e. I only put “first”, “second” etc. because its easy), and the values should be all the values of the array you shuffled (i.e. $my_array[0] to $my_array[99]). If you have php v. 5.4 or greater ignore that last part, you are all finished.

      //basic qualtrics webservice — echo query string
      $s1 = “”;
      $s2 = “”;
      $s3 = “”;
      $s4 = “”;
      $my_array = array($s1,$s2,$s3,$s4);
      shuffle($my_array);
      //echo(“first=$my_array[0]&second=$my_array[1]&third=$my_array[2]&fourth=$my_array[3]”);
      //For PHP versions greater than 5.3
      echo(http_build_query($my_array, “order_”, “&”, PHP_QUERY_RFC3986));

      If you cannot host the file, you can create the file for me, and I can upload it. Know that I currently have PHP 5.3 installed, so you’ll need to do that extra step mentioned above.

      In qualtrics you would create a Web Service element in the survey flow, put in the correct URL, and then test it. When tested it gives you the option to store the results as EDEs.

      3) In qualtrics, create 100 unique blocks. Each block will show one image and whatever questions you want to ask. Create a randomizer that assigns each URL to a unique embedded data element (EDE), and randomly selects 10 of them. Create a second randomizer with 100 branch elements in it. The randomizer should be set to randomly show all 100 elements. Each brand should have the logic IF [EDE name] IS NOT EMPTY. Then inside the branch is the block element corresponding to whatever image URL you are trying to show. This will randomly show 10 blocks. The other 90 will not show, because the first randomizer never set a value for those 90 EDEs and they are thus empty.

    • Thank you so much for this information!!! I stumbled across your blog trying to resolve the same question and had been very much stumped.

      Good and smart people like you is really what makes the research happen.

  2. Hi Andrew,

    I’m trying and failing to alter what you’ve written here to do something I need in Qualtrics. I’d like my question text to include a couple of sentences followed by a list of items. Each item is just text, and I want the items to be displayed in a random order. Any ideas? Thank you for this!

    • I can’t even copy and paste into the JS editor what you have in (1) without getting an unexpected token error.

    • The simplest fix, if you can work it somehow, would be to use the question randomization menu. You’d put each item in a separate question, randomize those questions, and have it display as many questions on the page as you need it. Of course, that means each page in that block will have that many questions on it, so you will probably need to put this in a separate block.

      If you want to go the JS route, the copy and paste problem may lie in something odd happening to the HTML of the post. Is it copying the line numbers? It shouldn’t be. Anyway, you can copy the code from GitHub instead (the link is in the post). Below should be most of your JS (including the function and comments)

      Edit: I cannot figure out how to get WordPress to display html in comments. Here is jsFiddle with what you’re looking for. The fiddle won’t work of course, but at least it won’t parse all the HTML tags. http://jsfiddle.net/andrewlong/rugepbu4/

      Make sure after the JS is all finished to go to your survey flow and add the embedded data element named itemOrder or else it won’t show in your data file.

      • FYI — the ‘unexpected token” error that KR was mentioning is due to the way the quotation marks are rendered by the HTML in the following lines:

        Qualtrics.SurveyEngine.setEmbeddedData(“****”,myArray[*]);

        Replace the quotes around first, second, etc with straight quotes in your JS editor and all should work. (JSLint.com to the rescue!)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: