参考:https://www.runoob.com/w3cnote/js-random.html
随机生成某个字段内的数值
ceil(x) 对数进行上舍入,即向上取整。floor(x) 对 x 进行下舍入,即向下取整。round(x) 四舍五入。random() 返回 0 ~ 1 之间的随机数,包含 0 不包含 1。Math.ceil(Math.random()*10); // 获取从 1 到 10 的随机整数,取 0 的概率极小。Math.round(Math.random()); // 可均衡获取 0 到 1 的随机整数。Math.floor(Math.random()*10); // 可均衡获取 0 到 9 的随机整数。Math.round(Math.random()*10); // 基本均衡获取 0 到 10 的随机整数,其中获取最小值 0 和最大值 10 的几率少一半。
生成 [n,m] 的随机整数
//生成从minNum到maxNum的随机数function randomNum(minNum,maxNum){switch(arguments.length){case 1:return parseInt(Math.random()*minNum+1,10);break;case 2:return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);break;default:return 0;break;}}
生成 [1,max] 的随机数
// max - 期望的最大值parseInt(Math.random()*max,10)+1;Math.floor(Math.random()*max)+1;Math.ceil(Math.random()*max);
生成 [0,max] 到任意数的随机数
// max - 期望的最大值parseInt(Math.random()*(max+1),10);Math.floor(Math.random()*(max+1));
生成 [min,max] 的随机数
// max - 期望的最大值// min - 期望的最小值parseInt(Math.random()*(max-min+1)+min,10);Math.floor(Math.random()*(max-min+1)+min);
随机生成某个字段内的数值并且限制某几个数值的和为固定数值
addList中是保存前几条数据的情况/*需要保证每个小时增加1250,每两分钟增加一次(还要求每次增加不超过100)如果使用随机数的情况,前29条数据是增加100以内的数字,则第30条增加的数据会很大,可能会超过100,可以使用下面的方式:将1个小时的数据和再次切割,变成10分钟的数据之和,即保证10分钟内增加208,每2分钟增加一次.则即使碰到最坏的情况,最后一个增加的值也就是128*/this.timer = setInterval(() => {// 随机数取[20+0, 20+40]let otherValue = 20 + Math.floor(Math.random()*(40+1));if(this.addList.length === 4) {const reduce = this.addList.reduce((all, cu) => all + cu)otherValue = ((208 - reduce) > 0 ? (208 - reduce) : 0);if(otherValue > 100) {otherValue = 100}this.addList = []console.log(otherValue)}else {console.log(this.addList.length, otherValue)this.addList.push(otherValue)}}, 1 * 1000 * 2)
