function shareMoeny(s_peopleCount, s_moeny) {
randomMoneyList = [];
for (let i = 0; i < s_peopleCount - 1; i++) { //随机的次数为人数-1 为最后一个人留位置
let random = createRandom(0, (s_moeny / s_peopleCount) * 2);//二倍均值法 拿到随机金额
s_moeny -= random;
randomMoneyList.push(random);
console.log("第" + parseInt(i + 1) + "人分得:" + random);
}
randomMoneyList.push(s_moeny);
return randomMoneyList;
}
//生成[min,max]的随机数
function createRandom(min, max) {
let backdataNumber = (Math.random() * (max - min + 1) + min).toFixed(2); //2位小数
return backdataNumber;
}
shareMoeny(10, 100);
网友评论