红包分配算法

162 阅读1分钟
function red(total, num) {
  const res = [];
  let _total = total;
  
  for (let i = 0; i < num - 1; i++) {
    let current = Math.random() * _total * ((i + 1) / num)
    current = +current.toFixed(2);
    _total -= current;
    res.push(current);
  }
  res.push(+_total.toFixed(2));
  console.log(res);
  return res;
}