php抽奖概率计算类
<?php /**
* 概率计算类
* 可用于抽奖等
*/
class Probability
{
/**
* 概率统计数据
* thing => chance
*/
var $data = array();
var $chance_count = 0;
function __construct($initdata = array()){
if(!empty($initdata)){
$this->data = $initdata;
foreach($initdata as $d){
$this->chance_count += $d['num'];
}
}
}
function addData($name, $chance){
$this->data[]=array('name'=>$name, 'num'=>$chance);
$this->chance_count += $chance;
}
function getOne(){
$index = rand(0, $this->chance_count);
foreach($this->data as $d){
$index = $index-$d['num'];
if($indexaddData('iphone',10);
$pro->addData('watch',30);
$pro->addData('$18',50);
$pro->addData('thank you',10);
$pro->addData('super big',1);
for($i=0;$igetOne()."\n";
}
以上就是php抽奖概率计算类的内容,更多相关内容请关注PHP中文网(www.php.cn)!
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











