适用于抽奖系统 /* * 一个抽奖类,精确到万分之一 * 三个步骤:1.接受一个中奖概率数组;2.接受一个抽奖种子;3.返回中奖等级 */class lottery { /* * 中奖概率数组,自动判断奖项数目 * 数组键值和为100,自动计算出不中奖的概率,若初始是超过100抛出一个错误 */ protected $_rate = array(); /* * 设置中奖概率, * @param array,中奖概率,以数组形式传入 */ public function setrate($rate = array(12.1, 34)) { $this->_rate = $rate; if (array_sum($this->_rate) > 100)//检测概率设置是否有问题 throw new exception('winning rate upto 100%'); if (array_sum($this->_rate) //定义未中奖情况的概率,用户给的概率只和为100时,则忽略0 $this->_rate[] = 100 - array_sum($this->_rate); } /* * 随机生成一个1-10000的整数种子,提交给中奖判断函数 * @return int,按传入的概率排序,返回中奖的项数 */ public function runonce() { return $this->judge(mt_rand(0, 10000)); } /* * 按所设置的概率,判断一个传入的随机值是否中奖 * @param int,$seed 10000以内的随机数 * @return int,$i 按传入的概率排序,返回中奖的项数 */ protected function judge($seed) { foreach ($this->_rate as $key => $value) { $tmparr[$key + 1] = $value * 100; } //将概率乘十后累计,以便随机选择,组合成 $tmparr[0] = 0; foreach ($tmparr as $key => $value) { if ($key > 0) { $tmparr[$key] += $tmparr[$key - 1]; } } for ($i = 1; $i if ($tmparr[$i - 1] return $i; //返回中奖的项数(按概率的设置顺序) } } }}$rate = array(33, 20, 2, 0.95, 12, 4.55);$a = new lottery;$a->setrate($rate);for ($i = 0; $i $b = $a->runonce(); @$rewards[$b]++;}unset($rewards['']);echo array_sum($rewards);?> 运行10000次,对比设置概率和中奖次数 设置概率中奖次数
%
%
%
%
%
%
复制代码