基于JavaScript实现微信抢红包功能
程序员文章站
2022-05-26 08:24:49
金额随机:额度在0.01和(剩余平均值*2)之间。
/**
* 抢红包
* @param {[number]} totalamount [总金额]
*...
金额随机:额度在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实现微信抢红包功能,希望对大家有所帮助