大佬教程收集整理的这篇文章主要介绍了php实现概率性随机抽奖代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1、初始数据:
权重越大,抽取的几率越高 [奖品1, 权重 5],[ 奖品2, 权重6], [ 奖品3, 权重 7], [ 奖品4, 权重2]
2、处理步骤:
1)N = 5 + 6 + 7 + 2 = 20 2)然后取1-N的随机数M 3)界定各 奖品的权重范围值 奖品 1 : 1-5 ; 奖品2 : 6-11; 奖品3: 12-18; 奖品4: 19-20 4) 如果M在某个奖品的权重范围值内,标识这个奖品被抽取到
php;">权重范围区间起始值
protected $start = 0;
权重范围区间结束值
protected $end = 0;
public function __construct($id,$weight,$Name) {@H_673_21@ if (!$id) {@H_673_21@ throw new Exception('奖品ID为空.');@H_673_21@ }@H_673_21@ $this->id = $id;@H_673_21@ $this->weight = $weight ? $weight : 0;@H_673_21@ $this->name = $name ? $name : '随机奖品' . $id;@H_673_21@ }
id
public function getId() {@H_673_21@ return $this->id;@H_673_21@ }
权重
public function getWeight() {@H_673_21@ return $this->weight;@H_673_21@ }
设置权重范围区间
public function setRange($start,$end) {@H_673_21@ $this->start = $start;@H_673_21@ $this->end = $end;@H_673_21@ }
判断随机数是否在权重范围区间
public function inRange($num) {@H_673_21@ return ($num >= $this->start) && ($num <= $this->end);@H_673_21@ }@H_673_21@ }
/**
protected $items = array();
public function addItem(Prize $item) {@H_673_21@ $this->items[$item->getId()] = $item;@H_673_21@ return $this;@H_673_21@ }
public function removeItem($itemId) {@H_673_21@ if (isset($this->items[$itemId])) {@H_673_21@ unset($this->items[$itemId]);@H_673_21@ }@H_673_21@ return $this;@H_673_21@ }
public function updateItem(Prize $item) {@H_673_21@ if (isset($this->items[$item->getId()])) {@H_673_21@ $this->items[$item->getId()] = $item;@H_673_21@ }@H_673_21@ return $this;@H_673_21@ }
public function getItems() {@H_673_21@ return $this->items;@H_673_21@ }
public function getVisibleItems() {@H_673_21@ $items = array();@H_673_21@ foreach ($this->items as $item) {@H_673_21@ if ($item->getWeight()) {@H_673_21@ $items[$item->getId()] = $item;@H_673_21@ }@H_673_21@ }@H_673_21@ return $items;@H_673_21@ }
public function count() {@H_673_21@ return count($this->items);@H_673_21@ }
public function getIterator() {@H_673_21@ return new ArrayIterator($this->items);@H_673_21@ }@H_673_21@ }
/**
public function __construct(PrizePoll $poll) {@H_673_21@ if ($poll) {@H_673_21@ $this->setPoll($poll);@H_673_21@ }@H_673_21@ }
public function run(PrizePoll $poll) {@H_673_21@ $poll = $poll ? $poll : $this->poll;@H_673_21@ if ( ! $poll) {@H_673_21@ throw new Exception('奖池未初始化');@H_673_21@ }
if ($poll->count() <= 0) {
throw new Exception('奖池为空');
}
$items = $poll->getVisibleItems();
if (count($items) <= 0) {
throw new Exception('奖池为空');
}
$sum = 0;
foreach ($items as $item) {
$start = $sum + 1;
$sum += $item->getWeight();
$end = $sum;
# 设置奖品的权重范围区间
$item->setRange(<a href="http://code.js-code.com/tag/start/" target="_blank" class="keywords">$start</a>,$end);
}
# <a href="http://code.js-code.com/tag/suiji/" target="_blank" class="keywords">随机</a>数
$rand = $this->getRandNum(1,$sum);
# 区间段判断
foreach ($items as $item) {
if ($item->inRange($rand)) {
return $item;
}
}
return null;
}
public function getRandNum($min,$maX) {@H_673_21@ return mt_rand($min ? $min : 1,$maX);@H_673_21@ }
public function setPoll(PrizePoll $poll) {@H_673_21@ $this->poll = $poll;@H_673_21@ }@H_673_21@ }
try {@H_673_21@ $prizePoll = new PrizePoll();@H_673_21@ $prizePoll->addItem(new Prize(1,5))@H_673_21@ ->addItem(new Prize(2,6))@H_673_21@ ->addItem(new Prize(3,7))@H_673_21@ ->addItem(new Prize(4,2));
$turn = new SimpleTurn($prizePoll);@H_673_21@ $prize = $turn->run();@H_673_21@ var_dump($prizE);@H_673_21@ } catch (Exception $E) {@H_673_21@ print_r($E);@H_673_21@ }
以上是大佬教程为你收集整理的php实现概率性随机抽奖代码全部内容,希望文章能够帮你解决php实现概率性随机抽奖代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。