当前位置:首页 > 开发教程 > IT博文 > PHP技术 >

一个抽奖函数(自定义中奖项数和概率)

时间:2014-05-08 08:42 来源:互联网 作者:源码搜藏 收藏

?/* * 一个抽奖类,精确到万分之一 * 三个步骤:1.接受一个中奖概率数组;2.接受一个抽奖种子;3.返回中奖等级 */class Lottery { /* * 中奖概率数组,自动判断奖项数目 * 数组键值和为100,自动计算出不中奖的概率,若初始是超过100抛出一个错误 */ protected
<
/*
 * 一个抽奖类,精确到万分之一
 * 三个步骤: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)
        //定义未中奖情况的概率,用户给的概率只和为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 < count($tmpArr); $i++) {
            if ($tmpArr[$i - 1] < $seed && $seed <= $tmpArr[$i]) {
                return $i; //返回中奖的项数(按概率的设置顺序)
            }
        }
    }

}

$rate = array(33, 20, 2, 0.95, 12, 4.55);

$a = new Lottery;
$a->setRate($rate);
for ($i = 0; $i <= 10000; $i++) {
    $b = $a->runOnce();
    @$rewards[$b]++;
}
unset($rewards['']);
echo array_sum($rewards);
>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
    </head>
    <body>
        <table>
            <thead>运行10000次,对比设置概率和中奖次数</thead>
            <tr><th>设置概率</th><th>中奖次数</th></tr>
            <tr><td><php echo $rate[0]; >%</td><td><php echo $rewards[1] ></td></tr>
            <tr><td><php echo $rate[1]; >%</td><td><php echo $rewards[2] ></td></tr>
            <tr><td><php echo $rate[2]; >%</td><td><php echo $rewards[3] ></td></tr>
            <tr><td><php echo $rate[3]; >%</td><td><php echo $rewards[4] ></td></tr>
            <tr><td><php echo $rate[4]; >%</td><td><php echo $rewards[5] ></td></tr>
            <tr><td><php echo $rate[5]; >%</td><td><php echo $rewards[6] ></td></tr>
            <tr><td><php echo 'miss'; ></td><td><php echo $rewards[7] ></td></tr>
        </table>
    </body>
</html>


PHP技术阅读排行

最新文章