Open Vitaminaq opened 2 years ago
function createWeightedRandom_O1(input) {
const len = input.length;
return function() {
const list = [];
let total = 0;
for (let i = 0; i < len; i++) {
total = total + input[i];
Array.prototype.push.apply(list, new Array(input[i]).fill(i));
}
return list[Math.floor(Math.random() * total)];
}
}
此函数接收一个整数数组 input, 此数组:
返回一个随机函数, 此随机函数:
分别按以下两种要求实现该函数: