标签:blog http java strong for io
写了一个抽奖的jquery插件和计算概率的方法, 结合起来就是一个简单的概率抽奖, 不过实际项目中基本不会把抽奖概率的计算放在前端处理~。
概率计算
function Probability(conf) { this.probArr = conf || []; this.range = [], this.len = this.probArr.length; if (this.len > 0) { this.init(); } } Probability.prototype = { init: function() { this.setRange(); }, get: function() { var len = this.len, range = this.range, last, randNum, i = 0; if (len === 0) { return; } else if(len === 1) { return 0; } last = range[len -1]; randNum = Math.floor(last* Math.random()); for (; i < len; i++) { if (randNum < range[i]) {
break; } } return i; }, setRange: function() { var range = [], probArr = this.probArr, i = 0, len = probArr.length; for(; i<len; i++) { var now = probArr[i], last = range[i-1] || 0; range.push(now+last); }
this.range = range; } };
博客园markdown代码之间不能有空行真的很无语啊
javascript抽奖插件+概率计算,布布扣,bubuko.com
标签:blog http java strong for io
原文地址:http://www.cnblogs.com/blackwood/p/3842695.html