00:00/
发布于2018-02-22 / 3780次浏览
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
function Redpaper(totalMoney,totalSize){
this.totalMoney = totalMoney;
this.totalSize = totalSize;
this.remainMoney = totalMoney;
this.remainSize = totalSize;
this.count=0;
this.sum=0;
}
Redpaper.prototype={
tofixed:function(num,s){
let times = Math.pow(10, s);
let des = num * times + 0.5;
des = parseInt(des, 10) / times;
return des + '';
},
getMoney:function(){
let min=0.01;
let max=this.remainSize;
this.count++;
if(this.remainSize>1){
let randomMoney=Math.floor(Math.random()*parseInt((this.remainMoney/this.remainSize*2)*100)+1)/100;
this.sum=Number(this.tofixed(this.sum+randomMoney,2));
this.remainMoney=Number(this.tofixed(this.remainMoney-randomMoney,2));
this.remainSize--;
console.log("第"+this.count+"次的红包为: "+randomMoney+" 元");
this.getMoney();
}else{
this.sum=Number(this.tofixed(this.sum+this.remainMoney,2));
console.log("第"+this.count+"次的红包为: "+this.remainMoney+" 元");
console.log("共 "+this.sum+" 元的红包已发完");
}
}
}
let redpaper = new Redpaper(20,5);
redpaper.getMoney();
</script>
</body>
</html>
模拟20个红包五个人分的情况:
算法还是挺公平的:
模拟10000次10元红包5人分的情况:
模拟10000次20元红包5分人的情况:
各位大佬,有什么办法能优雅的在wordpress中嵌入代码啊。
不得不说wordpress对代码的支持就是:代码如屎
博主好帅呀~

> 不得不说wordpress对代码的支持就是:代码如屎
你这是在侮辱屎
自己写博客~~~
PHP 写一个博客不是很方便的吗
@yukun 大佬你来了呀
typecho的我现在也在寻求方法嵌入代码…用了各种插件依旧没用的
@xema 还是得早点脱坑,自己开发博客系统才是王道。
@admin 膜拜自己开发博客的大佬。
@广树 我不是 我没有 别瞎说啊~
@admin 哈哈哈,一般大佬不都是这么说的吗?