Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

Randomization

Thomas J. Leeper edited this page Apr 26, 2015 · 4 revisions

Randomizing the Content of a HIT

Sometimes it makes sense to display different content to different workers within the same HIT. With an HTMLQuestion HIT, this is possible using JavaScript. There are two ways "randomly" display content:

  1. Display some content generated randomly each time the HIT is loaded by a worker. This is (pseudo-)random but has a significant downside: the content shown to the worker when they "preview" the HIT will likely be different from the content shown to the worker after they accept the HIT. This means it will be difficult for a requester to know exactly what the worker was seeing. This can still work if you write the JavaScript to not display anything to the worker if they are in "preview" mode.

  2. Display some content based on the worker's WorkerId. This is not random per se, but it means that the worker will always see the same content for a HIT regardless of whether they are in preview mode or not. It also means that if one were to create multiple HITs in a "batch", with each HIT displaying "random" content based on WorkerId, it would be possible to "randomly" assign the same worker to the same condition across HITs. This could be useful, for example, where multiple HITs are part of the same experiment.

Some code examples for both of these strategies is included below.

Example: Random content display

<script type="text/javascript">
/* DEFINE FUNCTION TO EXTRACT PARAMETERS FROM URL */
function turkGetParam( name ) { 
  var regexS = "[\?&]"+name+"=([^&#]*)"; 
  var regex = new RegExp( regexS ); 
  var tmpURL = fullurl; 
  var results = regex.exec( tmpURL ); 
  if( results == null ) { 
    return ""; 
  } else { 
    return results[1];    
  } 
}

/* THIS IS THE LINE TO CAPTURE THE ACTUAL URL: */
var fullurl = window.location.href;

/* ASSIGNS THE URL PARAMETERS TO JAVASCRIPT VARIABLES */
var assign = turkGetParam('assignmentId');
var hit = turkGetParam('hitId');
var worker = turkGetParam('workerId');

/* WHAT TO DO IF THE WORKER IS PREVIEWING THE HIT: */
if(assign=="ASSIGNMENT_ID_NOT_AVAILABLE")
    {
    document.write("<p style='font-weight:bold;text-align:center;'>Link will become available once you accept the HIT.</p>");
    }
/* CHECK workerId AGAINST THE LIST OF INELIGIBLE WORKERS */
else {
/* THE BELOW RANDOMIZES INTO A TREATMENT CONDITION */
/* DEPENDING ON THE EXPERIMENT THE "rtext[i]" RANDOMIZATION COULD BE USED TO DO MULTIPLE THINGS */
var rtext = new Array ();
/* THE NUMBER OF VALUES HERE DEPENDS ON NUMBER OF CONDITIONS */
rtext[0] = "Test1";
rtext[1] = "Test2";
rtext[2] = "Test3";
rtext[3] = "Test4";
rtext[4] = "Test5";
rtext[5] = "Test6";
rtext[6] = "Test7";
/* THE MULTIPLIER WILL DEPEND ON THE NUMBER OF CONDITIONS */
var i = Math.floor(7*Math.random());
/* RECORDS THE RANDOMIZATION IN A HIDDEN FORM FIELD */
document.getElementById("random").value = rtext[i];
/* DISPLAY RANDOMIZED LINK */
var surveylink = new String("http://www.test.com/" + rtext[i]);
document.write("<a href=\"" + surveylink + "\">Complete this survey</a><br />");
}
</script>

Example: Nonrandom content display based on WorkerId

<form name='mturk_form' method='post' id='submitform' action='https://www.mturk.com/mturk/externalSubmit'>
    <input type='hidden' value='' name='assignmentId' id='assignmentId' />
    <input type='hidden' value='' name='nonrandom' id='nonrandom' />
    <script>
        /* function to extract parameters from URL */
        function turkGetParam( name ) { 
          var regexS = "[\?&]"+name+"=([^&#]*)"; 
          var regex = new RegExp( regexS ); 
          var tmpURL = fullurl; 
          var results = regex.exec( tmpURL ); 
          if( results == null ) { 
            return ""; 
          } else { 
            return results[1];    
          } 
        }

        /* THIS IS THE LINE TO CAPTURE THE ACTUAL URL: */
        var fullurl = window.location.href;

        /* ASSIGNS THE URL PARAMETERS TO JAVASCRIPT VARIABLES */
        var assign = turkGetParam('assignmentId');
        var hit = turkGetParam('hitId');
        var worker = turkGetParam('workerId');

        if(assign=="ASSIGNMENT_ID_NOT_AVAILABLE")
            {
            document.write("<p style='font-weight:bold;text-align:center;'>The link is only available once you accept the HIT.</p>");
            }
        else
            {
            /* THE BELOW NON-RANDOMIZES INTO A TREATMENT CONDITION, BASED UPON workerId
               FIRST MAKE workerId UPPERCASE
               CONVERTS LAST LETTER/DIGIT OF workerId INTO ASCII IN 48-57,65-90 (36 alnum characters):
            */
            var ascii = worker.toUpperCase().charCodeAt(worker.length-1);
            /* BELOW CODE CONVERTS ASCII CODE TO A VARIABLE j ON 1-36 SCALE */
            var j;
            if (ascii <=57) {
                j = ascii-47;
                }
            else {
                j = ascii-54;
                }
            /* TRANSLATE DIGIT INTO A CONDITION NUMBER BASED ON NUMBER OF CONDITIONS*/
            var nconditions = 5;
            var i = j % nconditions;
            var sid = new Array ();
            sid[0] = "surveyid1";
            sid[1] = "surveyid2";
            sid[2] = "surveyid3";
            sid[3] = "surveyid4";
            sid[4] = "surveyid5";
            
            document.getElementById("nonrandom").value = sid[i];
            document.getElementById('assignmentId').value = assign;
            var surveylink = new String("http://www.example.com/?SID=" + sid[i] + "&workerId=" + worker + "&assignmentId=" + assign);
            
            /* DISPLAY LINK */
            document.write("<p style='font-weight:bold;text-align:center;'><a href=\"" + surveylink + "\" target=\"_blank\">Complete the Survey Here!</a></p><br />");
            document.write("<p>Please enter the completion code from last page of the survey here:&nbsp;&nbsp;&nbsp;<input type='text' id='complete' name='complete' />&nbsp;&nbsp;&nbsp;<input type='submit' id='submitButton' value='Submit' /></p>");
            }
    </script>
</form>