Rumah > php教程 > PHP源码 > php随机抽奖

php随机抽奖

PHP中文网
Lepaskan: 2016-05-26 08:19:24
asal
1486 orang telah melayarinya

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

<?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();
    }
}
Salin selepas log masuk

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
Salin selepas log masuk

                   

                   

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan