var arr = getRandomNum(1000, 60);
console.log(arr);
console.log(arr.reduce((pre, v) => pre + v, 0));
function getRandomNum(num, times) {
let res = [];
if (times === 1) {
res.push(num);
return res;
};
let max = num / times * 2;
let current = 1 + (~~(Math.random() * max - 1));
res.push(current);
return res.concat(getRandomNum(num - current, --times));
}
采用 https://www.zhihu.com/questio... 里讨论的微信分红包算法,达到较为平均的随机分配。