JavaScript指定红包金额的算法怎样实现?
Admin 2021-09-17 群英技术资讯 2672 次浏览
今天给大家分享一个比较有趣的实例,相信很多朋友都有抢过微信红包吧,有的人红包抢到的金额的多,有的人抢到的少,那么大家是否有好奇这是怎样实现的,下面小编就给大家介绍一下用JavaScript实现指定红包金额的算法,感兴趣的朋友就继续往下看吧。


/*
param: float, int, int, float
param1:红包金额总额
param2:红包数目
param3:指定特殊红包
param4:指定特殊红包金额
*/
let getPrize = function(total, number, index, volume){
let allowance = total - volume;
let arr = [];
let i = 0;
while(i < number - 2){
// 指定[0.01,allowance-(i*0.01))
let temp = (Math.random()*(allowance - (number - 1 - i) * 0.01) + 0.01).toFixed(2);
// if(temp < 0) console.log(`temp:${temp}`);
temp = temp <= 0 ? 0.01 : temp;
arr.push(parseFloat(temp));
allowance = parseFloat((allowance - temp).toFixed(2));
i++;
// console.log(`arr:${arr}, i:${i}`);
// 如果出现小于0的分配情况,剥削强者均分
if(allowance <= 0){
// console.log(`alowance:${allowance}`);
let w = arr.filter((val,index)=>{
// console.log(`val:${val}`);
if(val > 0.01){
arr[index] = parseFloat((arr[index] - 0.01).toFixed(2));
return val;
}
});
if(w.length == 0){
allowance = 0;
}else{
allowance = 0.01;
}
}
}
// 最后一个放入
arr.push(parseFloat(allowance.toFixed(2)));
let result = arr;
return result.slice(0, index).concat(parseFloat(volume), result.slice(index));
}
测试样例
// Test sample
for(let m = 0; m < 10000; m++){
let total = (Math.random()*100 + 0.01).toFixed(2);
let number = Math.floor(Math.random()*20 +2);
while(total / number < 0.01){
number = Math.floor(Math.random()*20 +2);
}
let index = Math.floor(Math.random()*(number - 1));
let volume = (Math.random()*(total - 0.01*(number-1))+0.01).toFixed(2);
while(volume >= total || volume + 0.01*(number-1) > total){
// console.log(`xx:${volume}`);
volume = (Math.random()*(total - 0.01*(number-1))+0.01).toFixed(2);
volume = volume <= 0 ? 0.01 : volume;
}
let test = getPrize(total, number, index, volume);
// console.log(test);
let sum = test.reduce((total,val)=>total+=parseFloat(val));
sum = sum.toFixed(2);
if(sum !== total) {
console.log(`volume:${volume}, total: ${total}, number: ${number}`);
console.log(`sum:${sum}`);
console.log(test);
}
test.map((val,index)=>{
if(val <= 0 && index !== test.length - 1){
console.log(`volume:${volume}, total: ${total}, number: ${number}`);
console.log(`sum:${sum}`);
console.log(test);
}
});
}
以上就是用JavaScript实现指定红包金额算法的介绍,上述代码仅供参考,感兴趣的朋友可以了解一下,希望本文对大家学习JavaScript有帮助,想要了解更多python的mysql模糊查询的内容,请浏览其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
js多维数组变一维有哪些方法?其实多维数组转一维数组的方法有很多,下文给大家分享了一些方法和实例,有需要的朋友可以参考,另外下文还简单介绍了数组去重和排序,感兴趣的朋友就接着往下看吧。
面向对象语言中 this 表示当前对象的一个引用。但在 JavaScript 中 this 不是固定不变的,它会随着执行环境的改变而改变,在方法中,this 表示该方法所属的对象,如果单独使用,this 表示全局对象
这篇文章主要给大家分享的是Vue3的几个优势,Vue3仍然在源码、性能和语法 API 三个大的方面进行了优化,下面我们一起进入文章看看具体详情吧
高阶函数就是函数式编程的一种实现方案,通常把接受一个或多个函数作为参数,或者返回一个函数的函数叫做高阶函数。本文主要介绍vue常用高阶函数以及案例,感兴趣的朋友继续往下看吧。
这篇文章主要为大家介绍了让chatgpt将html中的图片转为base64方法示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008