1、初始數(shù)據(jù):
權(quán)重越大,抽取的幾率越高
[獎(jiǎng)品1, 權(quán)重 5], [ 獎(jiǎng)品2, 權(quán)重6], [ 獎(jiǎng)品3, 權(quán)重 7], [ 獎(jiǎng)品4, 權(quán)重2]
2、處理步驟:
1)N = 5 + 6 + 7 + 2 = 20
2)然后取1-N的隨機(jī)數(shù)M
3)界定各 獎(jiǎng)品的權(quán)重范圍值 獎(jiǎng)品 1 : 1-5 ; 獎(jiǎng)品2 : 6-11; 獎(jiǎng)品3: 12-18; 獎(jiǎng)品4: 19-20
4) 如果M在某個(gè)獎(jiǎng)品的權(quán)重范圍值內(nèi),標(biāo)識(shí)這個(gè)獎(jiǎng)品被抽取到
<?php/** * 獎(jiǎng)品 */class Prize { # ID public $id = null; # 權(quán)重 public $weight = null; # 獎(jiǎng)品名 public $name = null; # 權(quán)重范圍區(qū)間起始值 protected $start = 0; # 權(quán)重范圍區(qū)間結(jié)束值 protected $end = 0; public function __construct($id, $weight, $name) { if (!$id) { throw new Exception('獎(jiǎng)品ID為空.'); } $this->id = $id; $this->weight = $weight ? $weight : 0; $this->name = $name ? $name : '隨機(jī)獎(jiǎng)品' . $id; } # id public function getId() { return $this->id; } # 權(quán)重 public function getWeight() { return $this->weight; } # 設(shè)置權(quán)重范圍區(qū)間 public function setRange($start, $end) { $this->start = $start; $this->end = $end; } # 判斷隨機(jī)數(shù)是否在權(quán)重范圍區(qū)間 public function inRange($num) { return ($num >= $this->start) && ($num <= $this->end); }} /** * 獎(jiǎng)品池 */class PrizePoll implements IteratorAggregate, Countable { # 獎(jiǎng)品集 protected $items = array(); # 加入獎(jiǎng)品 public function addItem(Prize $item) { $this->items[$item->getId()] = $item; return $this; } # 刪除獎(jiǎng)品 public function removeItem($itemId) { if (isset($this->items[$itemId])) { unset($this->items[$itemId]); } return $this; } # 更新獎(jiǎng)品 public function updateItem(Prize $item) { if (isset($this->items[$item->getId()])) { $this->items[$item->getId()] = $item; } return $this; } # 獲取所有獎(jiǎng)品 public function getItems() { return $this->items; } # 所有所有可用獎(jiǎng)品(如果權(quán)重為0,說(shuō)明這個(gè)獎(jiǎng)品永遠(yuǎn)不可能抽到) 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); }} /** * 簡(jiǎn)單的抽獎(jiǎng)?lì)?*/class SimpleTurn { # 獎(jiǎng)池 protected $poll = null; public function __construct(PrizePoll $poll) { if ($poll) { $this->setPoll($poll); } } # 抽獎(jiǎng) public function run(PrizePoll $poll) { $poll = $poll ? $poll : $this->poll; if ( ! $poll) { throw new Exception('獎(jiǎng)池未初始化'); } if ($poll->count() <= 0) { throw new Exception('獎(jiǎng)池為空'); } $items = $poll->getVisibleItems(); if (count($items) <= 0) { throw new Exception('獎(jiǎng)池為空'); } $sum = 0; foreach ($items as $item) { $start = $sum + 1; $sum += $item->getWeight(); $end = $sum; # 設(shè)置獎(jiǎng)品的權(quán)重范圍區(qū)間 $item->setRange($start, $end); } # 隨機(jī)數(shù) $rand = $this->getRandNum(1, $sum); # 區(qū)間段判斷 foreach ($items as $item) { if ($item->inRange($rand)) { return $item; } } return null; } # 獲取隨機(jī)數(shù) public function getRandNum($min, $max) { return mt_rand($min ? $min : 1, $max); } # 設(shè)置獎(jiǎng)池 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);}