将数字100拆分为10个随机数;
<script type="text/javascript">
let redpackage = function(){
let sum =100;
let money;
for(let i =0;i<15;i++){
if(i===15){
money=sum;
}else{
money=(Math.random()*sum).toFixed(2);
}
sum=sum-money;
console.log("第"+i+"位"+money+"元")
console.log(sum);
}
}
</script>
打印出来的结果;
redpackage();
test.html:30 第0位38.65元
test.html:31 61.35
test.html:30 第1位41.91元
test.html:31 19.440000000000005
test.html:30 第2位15.38元
test.html:31 4.060000000000004
test.html:30 第3位3.25元
test.html:31 0.810000000000004
test.html:30 第4位0.65元
test.html:31 0.16000000000000403
test.html:30 第5位0.03元
test.html:31 0.13000000000000403
test.html:30 第6位0.02元
test.html:31 0.11000000000000403
test.html:30 第7位0.03元
test.html:31 0.08000000000000403
test.html:30 第8位0.03元
test.html:31 0.05000000000000403
test.html:30 第9位0.01元
test.html:31 0.040000000000004025