金额随机:额度在0.01和(剩余平均值*2)之间。

/**
 * 抢红包
 * @param {[number]} totalAmount [总金额]
 * @param {[number]} totalPeople [总人数]
 * @return {[Array]}       [每个人抢到的金额]
 */
function assign(totalAmount, totalPeople){
  var remainAmount = +totalAmount;
  var remainPeople = +totalPeople;
  var arr = [];
  while(remainPeople > 0){
    let num = scramble(remainAmount, remainPeople);
    remainAmount = remainAmount - num;
    remainPeople--;
    arr.push(num);
  }
  return arr;
}
function scramble(remainAmount, remainPeople){
  if(remainPeople === 1){
    return +remainAmount.toFixed(2);
  }
  let max = ((remainAmount / remainPeople) * 2 - 0.01).toFixed(2);
  let min = 0.01;
  let range = max - min;
  let rand = Math.random();
  let num = min + Math.round(rand * range); //四舍五入
  return num;
}

总结

以上所述是小编给大家介绍的基于基于JavaScript实现微信抢红包功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com