首页 >php教程 >PHP源码 >php随机抽奖

php随机抽奖

PHP中文网
PHP中文网原创
2016-05-26 08:19:241502浏览

按照设定的概率,得到随机抽奖的结果。 

<?php 
/**
 * 抽奖工具
 */
class lottery_tool {

    protected static $awardsArr;
    protected static $proField = &#39;probability&#39;;
    protected static $proSum = 0;
    protected static $checkAward = false;

    const SUCCESS_CODE = 0;
    const FAIL_CODE = -1;

    //检查抽奖数据
    protected static function checkAwards(){

    	if (!is_array(self::$awardsArr) || empty(self::$awardsArr)) {
    		return self::$checkAward = false;
    	}
        
        self::$proSum = 0;

    	foreach (self::$awardsArr as $_key => $award) {
    		self::$proSum += $award[self::$proField];
        }

    	if (empty(self::$proSum)) {
            return self::$checkAward = false;
    	}

        return self::$checkAward = true;
    }
 
    protected static function successRoll($rollKey){
    	return array(&#39;code&#39; => self::SUCCESS_CODE, &#39;roll_key&#39; => $rollKey, &#39;msg&#39; => &#39;roll success&#39;);
    }

    protected static function failRoll($msg = &#39;roll fail&#39;){
    	return array(&#39;code&#39; => self::FAIL_CODE, &#39;msg&#39; => $msg );
    }

    //抽奖
    public static function roll () {

    	if (false == self::$checkAward) {
    		return self::failRoll(&#39;awards data is not the right format!&#39;);
    	}

    	$result = mt_rand(0, self::$proSum);
        $proValue = 0;

    	foreach (self::$awardsArr as $_key => $value) {
    		$proValue += $value[self::$proField]; 
    		if ($result <= $proValue) {
    			return self::successRoll($_key);
    		}
    	}
    	return self::failRoll(&#39;wrong&#39;);
    } 

    //改变概率字段名
    public static function setProField($field = null) {
    	if (!empty($field)) {
    		self::$proField = $field;
    	}
    }

    //设置奖品
    public static function setAwards($awards){
        self::$awardsArr = $awards;
        self::checkAwards();
    }
}

forexample 

$awards = array(
    &#39;0&#39; => array(&#39;pro&#39; => 15, &#39;info&#39; => &#39;15%的可能性&#39;),
    &#39;1&#39; => array(&#39;pro&#39; => 25, &#39;info&#39; => &#39;25%的可能性&#39;),
    &#39;2&#39; => array(&#39;pro&#39; => 40, &#39;info&#39; => &#39;40%的可能性&#39;),
    &#39;3&#39; => array(&#39;pro&#39; => 20, &#39;info&#39; => &#39;20%的可能性&#39;),
    );

lottery_tool::setProField(&#39;pro&#39;);
lottery_tool::setAwards($awards);

$result = array();

for ($i = 10000; $i --;) {
    $result[] = lottery_tool::roll();
}

foreach ($result as $key => $value) {
    $awards[$value[&#39;roll_key&#39;]][&#39;num&#39;] ++;
}

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump($awards);
//结果:
array
  0 => 
    array
      &#39;pro&#39; => int 15
      &#39;info&#39; => string &#39;15%的可能性&#39; (length=15)
      &#39;num&#39; => int 1596
  1 => 
    array
      &#39;pro&#39; => int 25
      &#39;info&#39; => string &#39;25%的可能性&#39; (length=15)
      &#39;num&#39; => int 2484
  2 => 
    array
      &#39;pro&#39; => int 40
      &#39;info&#39; => string &#39;40%的可能性&#39; (length=15)
      &#39;num&#39; => int 3939
  3 => 
    array
      &#39;pro&#39; => int 20
      &#39;info&#39; => string &#39;20%的可能性&#39; (length=15)
      &#39;num&#39; => int 1981

                   

                   

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn