每个红包应当多少钱?
你希望拿多少钱?
预算需要是100元整
function generateValue(max) {
return Math.random() * max;
}
function generateValue(max) {
return Math.random() * max;
}
需求
结论
const maxValue = 100
const totalTimes = 1000
const distribution = {}
const recordSum = (value) => {
const count = distribution[value] || 0
distribution[value] = count + 1
}
const random = () => Math.random() * maxValue >> 0
const doOnce = () => compose(recordSum, sum, times)(random, totalTimes)
times(doOnce, 10000)
toPairs(distribution).forEach((arr) => console.log(`${arr[0]}, ${arr[1]}`))
public static double getRandomMoney(LeftMoneyPackage _leftMoneyPackage) {
// remainSize 剩余的红包数量
// remainMoney 剩余的钱
if (_leftMoneyPackage.remainSize == 1) {
_leftMoneyPackage.remainSize;
return (double) Math.round(_leftMoneyPackage.remainMoney * 100) / 100;
}
Random r = new Random();
double min = 0.01;
double max = _leftMoneyPackage.remainMoney / _leftMoneyPackage.remainSize * 2;
double money = r.nextDouble() * max;
money = money <= min ? min : money;
money = Math.floor(money * 100) / 100;
_leftMoneyPackage.remainSize--;
_leftMoneyPackage.remainMoney -= money;
return money;
}