我正在进行网络研究,其中参与者将按不同网页的形式按比例随机分配给治疗组。我有一个研究登录页面,我计划在此页面上根据样本量范围内的随机整数将参与者定向到不同的页面,从而将他们分配给不同的治疗组。例如,对于n = 100的研究,此代码可能类似于:
/// Function for randomly shuffling array
function shuffle(array) {
array.sort(() => Math.random() - 0.5);
}
const STUDY_LEN = 100; /// Study n
const arr_0 = new Array(STUDY_LEN/2).fill(0); /// Create array for treatment 1
const arr_1 = new Array(STUDY_LEN/2).fill(1); /// Create array for treatment 2
const arr_assign = arr_0.concat(arr_1); /// Concatenate treatment arrays for entire study assignment
arr_shuffle = shuffle(arr_assign); /// Randomize order of array
for (i = 0; i < STUDY_LEN-1; i++){
if (arr_shuffle[i] == 0){
//// Change hyperlink to web page for treatment 1
}
else{
//// Change hyperlink to web page for treatment 2
}
}
我如何在多次访问网页时存储这些变量(主要是随机分配的数组),以便我将参与者平均分配给每种治疗(治疗1中有50名参与者,治疗2中有50名参与者)?我的解决方案似乎不是一种将用户随机分配给不同治疗方法的有效方法,因此我愿意接受任何/所有建议。
如果要随机但平均地分发测试,则必须从服务器获取测试类型
function getTestType () {
if (Math.random() < 0.5) {
// This will ensure that the maximum number of occurences for the test A will be 50.
if (getNumberOfOccurencesOfTestA() < 50) {
return true; // Test A
}
}
// This will ensure that the maximum number of occurences for the test B will be 50.
if (getNumberOfOccurencesOfTestB() < 50) {
return false; // Test B
}
// Just return 'undefined' if both of the test already have 50 occurences each.
return;
}
““ getNumberOfOccurencesOfTestA”和“ getNumberOfOccurencesOfTestB”是查找变量的函数,该变量保持测试A和测试B分别出现的次数的状态,它可以是全局变量,数据库,文件等。