您好,登錄后才能下訂單哦!
小編給大家分享一下php如何實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
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); }
以上是“php如何實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。