php实现概率性随机抽奖代码,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在某个奖品的权重范围值内,标识这个奖品被抽取到
id = $id; $this->weight = $weight ? $weight : 0; $this->name = $name ? $name : '随机奖品' . $id; } # id public function getid() { return $this->id; } # 权重 public function getweight() { return $this->weight; } # 设置权重范围区间 public function setrange($start, $end) { $this->start = $start; $this->end = $end; } # 判断随机数是否在权重范围区间 public function inrange($num) { return ($num >= $this->start) && ($num end); }} /** * 奖品池 */class prizepoll implements iteratoraggregate, countable { # 奖品集 protected $items = array(); # 加入奖品 public function additem(prize $item) { $this->items[$item->getid()] = $item; return $this; } # 删除奖品 public function removeitem($itemid) { if (isset($this->items[$itemid])) { unset($this->items[$itemid]); } return $this; } # 更新奖品 public function updateitem(prize $item) { if (isset($this->items[$item->getid()])) { $this->items[$item->getid()] = $item; } return $this; } # 获取所有奖品 public function getitems() { return $this->items; } # 所有所有可用奖品(如果权重为0,说明这个奖品永远不可能抽到) public function getvisibleitems() { $items = array(); foreach ($this->items as $item) { if ($item->getweight()) { $items[$item->getid()] = $item; } } return $items; } # countable::count public function count() { return count($this->items); } # iteratoraggregate::getiterator() public function getiterator() { return new arrayiterator($this->items); }} /** * 简单的抽奖类 */class simpleturn { # 奖池 protected $poll = null; public function __construct(prizepoll $poll) { if ($poll) { $this->setpoll($poll); } } # 抽奖 public function run(prizepoll $poll) { $poll = $poll ? $poll : $this->poll; if ( ! $poll) { throw new exception('奖池未初始化'); } if ($poll->count() getvisibleitems(); if (count($items) getweight(); $end = $sum; # 设置奖品的权重范围区间 $item->setrange($start, $end); } # 随机数 $rand = $this->getrandnum(1, $sum); # 区间段判断 foreach ($items as $item) { if ($item->inrange($rand)) { return $item; } } return null; } # 获取随机数 public function getrandnum($min, $max) { return mt_rand($min ? $min : 1, $max); } # 设置奖池 public function setpoll(prizepoll $poll) { $this->poll = $poll; }} # 示例try { $prizepoll = new prizepoll(); $prizepoll->additem(new prize(1, 5)) ->additem(new prize(2, 6)) ->additem(new prize(3, 7)) ->additem(new prize(4, 2)); $turn = new simpleturn($prizepoll); $prize = $turn->run(); var_dump($prize);} catch (exception $e) { print_r($e);}
您可能感兴趣的文章:php抽奖小程序的实现代码php实现可以设置中奖概率的抽奖程序代码分享适用于抽奖程序、随机广告的php概率算法实例php解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题php转盘抽奖接口实例php编写的抽奖程序中奖概率算法php+jquery翻板抽奖功能实现
http://www.bkjia.com/phpjc/1087267.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1087267.htmltecharticlephp实现概率性随机抽奖代码,php概率抽奖代码 1、初始数据: 权重越大,抽取的几率越高 [奖品1, 权重 5], [ 奖品2, 权重6], [ 奖品3, 权...