-
Notifications
You must be signed in to change notification settings - Fork 17
Randomization
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:
-
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.
-
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.
<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><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>");
}
else {
// THE BELOW NON-RANDOMIZES INTO A TREATMENT CONDITION, BASED UPON workerId; IT IS CALLED AT PAGE onload
// BELOW CODE CONVERTS LAST DIGIT OF workerId INTO ASCII IN 48-57,65-90,97-122 (62 alnum characters):
var ascii = worker.charCodeAt(worker.length-1);
// BELOW CODE CONVERTS ASCII CODE TO A VARIABLE j ON 1-62 SCALE
var j;
if (ascii <=57) {
j = ascii-47;
}
else if (ascii <=90) {
j = ascii-54;
}
else {
j = ascii-60;
}
// BELOW CODE CONVERTS THE RESCALED j INTO PRE-DEFINED TREATMENT GROUPS
// CONVERSION WILL DEPEND ON NUMBER OF CONDITIONS AND HOW j (AND LAST LETTER OF WORKERID) TRANSLATES INTO A CONDITION
if (j>=1 && j<10) {
i = 0;
}
else if (j>=10 && j<20) {
i = 1;
}
else if (j>=20 && j<30) {
i = 2;
}
else if (j>=30 && j<40) {
i = 3;
}
else if (j>=40 && j<50) {
i = 4;
}
else if (j>=50 && j<60) {
i = 5;
}
else {
i = 5; // Have to deal with the remainder somehow
}
// BELOW CODE GENERATES NONRANDOM TREATMENT CONDITION
var ntext = new Array ();
ntext[0] = "Test0"; // THE NUMBER OF VALUES HERE DEPENDS ON NUMBER OF CONDITIONS
ntext[1] = "Test1";
ntext[2] = "Test2";
ntext[3] = "Test3";
ntext[4] = "Test4";
ntext[5] = "Test5";
// RECORDS THE NON-RANDOMIZATION IN A HIDDEN FORM FIELD
document.getElementById("nonrandom").value = ntext[i];
// DISPLAY RANDOMIZED LINK
var surveylink = new String("http://www.test.com/" + ntext[i]);
document.write("<a href=\"" + surveylink + "\">Complete this survey</a><br />");
</script>.