如何随机分配治疗组进行在线网络学习?

时间:2020-06-01 16:44:36

标签: javascript random variable-assignment

我正在进行一项网络研究,其中参与者将按照不同的网页形式按比例随机分配到治疗组。我有一个研究登录页面,我计划在此页面上根据样本量范围内的随机整数将参与者定向到不同的页面,从而将他们分配给不同的治疗组。例如,对于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名参与者)?我的解决方案似乎不是一种将用户随机分配给不同治疗方法的强大方法,因此我愿意接受任何/所有建议。

1 个答案:

答案 0 :(得分:1)

如果要随机但平均地分发测试,则必须从服务器获取测试类型

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分别发生的次数的状态,它可以是全局变量,数据库,文件等。 / p>