写了一个抽奖的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代码之间不能有空行真的很无语啊