const LUCKY_AIRDROP_PRIZE = [ { "id": 1, "prop": 16.2 }, { "id": 2, "prop": 16.2 }, { "id": 3, "prop": 16.2 }, { "id": 4, "prop": 16.2 }, { "id": 5, "prop": 8.8 }, { "id": 6, "prop": 8.8 }, { "id": 7, "prop": 8.8 }, { "id": 8, "prop": 8.8 }, ]; module.exports = { getPrizeInfo: function(){ const prize = utils.clone(LUCKY_AIRDROP_PRIZE); let propArr = []; for(let i = 0; i < prize.length; i++){ propArr.push(prize[i]['prop']) } return prize[this.probabilisticAlgorithm(propArr)]['id']; }, // 奖品概率算法 probabilisticAlgorithm: function (arr){ let leng = 0; for(let i=0; i < arr.length; i++){ leng += arr[i] //获取总数 } for(let i=0; i<arr.length; i++){ let random = parseInt(Math.random()*leng); //获取 0-总数 之间的一个随随机整数 if(random < arr[i]){ return i //如果在当前的概率范围内,得到的就是当前概率 } else { leng -= arr[i] //否则减去当前的概率范围,进入下一轮循环 } } } }
标签:奖品,prize,8.8,Javascript,prop,算法,let,16.2,id From: https://www.cnblogs.com/Essaycode/p/16984660.html