1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| var foo = function () { var getCurrentValue = function (list, value) { let msg = {index:0,count:0}; for (var j = 0; j < list.length; j++) { msg.index = j; msg.count = isRange(list[j], value); if (msg.count >= 0) { return msg; } } return msg; } var isRange = function range(array, value) { if(value==array[0]){ return 0; }else if (value > array[0] && value <= array[1]) { return array[2]; } else { return -1; } } var size = 37, value = 0, result = false, awardCount = 0, awardMax = 200,baseValue = 0.1,clock = -1,awardList = []; var list = [[5, 10, 0.15], [11, 15, 0.25], [16, 19, 0.35], [20, 23, 0.45], [24, 27, 0.55], [28, 30, 0.65], [31, 37, 0.70]]; for (var i = 1; i <= size; i++) { if (awardCount >= awardMax) { console.log("中奖超过" + awardMax + "次" + "不能再次中奖"); continue; } var message = getCurrentValue(list, i); if (message.count < 0) { console.log("等级为:", JSON.stringify(i) + "不符合条件"); continue; } if(clock!=message.index){ clock = message.index; value = baseValue; } if(result){ value = baseValue; }else { value += message.count; } var randomValue = Math.random(); if (randomValue <= value) { result = true; awardCount++; awardList.push(i) } else { result = false; } let output = i+" "+"当前概率:" + value.toFixed(3) + "随机数是:" + randomValue.toFixed(4) + "结果是:" + JSON.stringify(result ? "中奖!" : "很遗憾"); if(result){ console.log("%c"+output,'color:#0033ee;'); }else { console.log(output); }
} var lastStr = awardList.join(" 次、"); console.log("%c中奖总次数是:"+awardList.length+" 分别是:"+lastStr,'color:#0099ee;'); return awardList.length; } var totalList = [],totalCount = 10; function test() { for (var n = 0; n < totalCount; n++) { (function (j) { setTimeout(function () { var res = foo(); totalList.push(res); if(totalCount-1 == j){ var sum = totalList.reduce((previous, current) => current += previous); var avg = sum / totalList.length; console.log("数组:"+JSON.stringify(totalList)); console.log("%c总共模拟:"+n+"次 平均中奖次数:"+avg,'color:#ff99ee;'); } console.log(j); }, n*100) })(n); }
} test();
|