如何为JavaScript数据随机化器创建多个随机数数组?

时间:2019-02-11 08:46:13

标签: javascript multidimensional-array random

我正在用javascript为我教的历史课程构建数据随机化器。我希望用户输入表格形式的学生人数,周数和每位学生的分配网数量,然后通过单击按钮提交此数据。例如40名学生,12周,每名学生5个作业。因此,我希望程序生成一个40行的表,每个表包含5个从1到12的随机唯一数。我设法使该程序生成一行随机数(函数weekAssigned),但是后来我陷入了困境。如何重复使用weekAssigned函数来产生其他行?我试图使用二维数组,但是我不知道如何随机化2d数组中的每一行。我将非常感谢您的建议!我想我应该以某种方式利用for循环或2d数组,但是我不确定如何使用。

function getRandomWeek() {
    var x,y;
    x = document.getElementById("form1").elements["duration"].value; 
    y = Math.floor(Math.random() * x + 1);
    return y;
} 

function weeksAssigned() {
    var ints = [];
    var assignments = document.getElementById("form1").elements["assignments"].value; 
    while (ints.length < assignments) {                                                    
        var randomWeak = getRandomWeek();
        if (ints.indexOf(randomWeak)===-1) {   
          ints.push(randomWeak);
        }
    } 
    ints.sort(function(a, b){return a-b});
    return ints;
}

2 个答案:

答案 0 :(得分:0)

您需要做的就是循环C[1]并将值推入2d数组中。

为帮助您解决此问题,请检查以下代码:

weeksAssigned

希望这会有所帮助:)

答案 1 :(得分:0)

在这里,您可以通过遍历每个学生然后遍历每个作业来完成此任务。

我首先创建一个二维数组,其中填充了0,其行数与学生数相同,而列数与作业数一样const assignArr = Array.from({ length: assignments }, (_, i) => 0); const studentsArr = Array.from({ length: students }, (_, i) => [...assignArr]);

然后,您使用两个循环用随机的星期填充此数组。对于每个学生,您都要在循环开始时初始化可用的星期,然后将此数组传递给getRandomWeeks(),这会从数组中删除随机的一周,因此您不必为每个学生重复它们:

for (let student = 0; student < students; ++student) {
    const weeks = Array.from({ length: duration }, (_, i) => i);
    for (let work = 0; work < assignments; ++work) {
        studentsArr[student][work] = getRandomWeek(weeks) + 1;
    }
    studentsArr[student].sort((a, b) => a - b);
}

function getRandomWeek(weeks) {
  const weekIdx = Math.floor(Math.random() * weeks.length);
  return weeks.splice(weekIdx, 1).shift();
}

然后,您只需调用一个辅助函数来生成表。这是一个完整的演示。您可以编辑表单字段并查看表更新:

function update() {
  const form = document.getElementById('form');
  const duration = form.elements['weeks'].value;
  const students = form.elements['students'].value;
  const assignments = form.elements['assignments'].value;
  
  const assignArr = Array.from({ length: assignments }, (_, i) => 0);
  const studentsArr = Array.from({ length: students }, (_, i) => [...assignArr]);
  
  for (let student = 0; student < students; ++student) {
    const weeks = Array.from({ length: duration }, (_, i) => i);
    for (let work = 0; work < assignments; ++work) {
      studentsArr[student][work] = getRandomWeek(weeks) + 1;
    }
    studentsArr[student].sort((a, b) => a - b);
  }
  makeTable(studentsArr);
}

function getRandomWeek(weeks) {
  const weekIdx = Math.floor(Math.random() * weeks.length);
  return weeks.splice(weekIdx, 1).shift();
}

function makeTable(data) {
  const header = document.querySelector('#table thead tr');
  header.innerHTML = '';
  for (let i = 0; i < data[0].length + 1; ++i) {
    const colEl = document.createElement('th');
    colEl.textContent = i === 0 ? 'Assignments' : `#${i}`;
    header.appendChild(colEl);
  }
  const tbody = document.querySelector('#table tbody');
  tbody.innerHTML = '';
  data.forEach((row, i) => {
    const rowEl = document.createElement('tr');
    const col0 = document.createElement('td');
    col0.textContent = `Student #${i + 1}`;
    rowEl.appendChild(col0);
    for (let col of row) {
      const colEl = document.createElement('td');
      colEl.textContent = col;
      rowEl.appendChild(colEl);
    }
    tbody.appendChild(rowEl);
  });
}

update();
table {
  margin: 10px;
  border-collapse: collapse;
  text-align: center;
}

td, th {
  border: 1px solid black;
  padding: 3px;
}
<form id="form">
  Weeks <input type="number" name="weeks" value=12 onchange="update()">
  Students <input type="number" name="students" value=10 onchange="update()">
  Assignments <input type="number" name="assignments" value=5 onchange="update()">
</form>

<table id="table">
  <thead>
    <tr>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>