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權(quán)重計(jì)算方法代碼分享
- php編寫的抽獎(jiǎng)程序中獎(jiǎng)概率算法
- php實(shí)現(xiàn)可以設(shè)置中獎(jiǎng)概率的抽獎(jiǎng)程序代碼分享
- 適用于抽獎(jiǎng)程序、隨機(jī)廣告的PHP概率算法實(shí)例
- php抽獎(jiǎng)小程序的實(shí)現(xiàn)代碼
- 基于PHP實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)抽獎(jiǎng)小程序
- 基于PHP代碼實(shí)現(xiàn)中獎(jiǎng)概率算法可用于刮刮卡、大轉(zhuǎn)盤等抽獎(jiǎng)算法
- php實(shí)現(xiàn)自定義中獎(jiǎng)項(xiàng)數(shù)和概率的抽獎(jiǎng)函數(shù)示例
- PHP編程實(shí)現(xiàn)計(jì)算抽獎(jiǎng)概率算法完整實(shí)例
- php簡(jiǎn)單計(jì)算權(quán)重的方法示例【適合抽獎(jiǎng)?lì)悜?yīng)用】
相關(guān)文章
zend framework配置操作數(shù)據(jù)庫(kù)實(shí)例分析
zend framework項(xiàng)目建立后,看了下zend framework配置操作數(shù)據(jù)庫(kù),本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12PHP+MariaDB數(shù)據(jù)庫(kù)操作基本技巧備忘總結(jié)
這篇文章主要介紹了PHP+MariaDB數(shù)據(jù)庫(kù)操作基本技巧,結(jié)合實(shí)例形式總結(jié)分析了PHP+MariaDB數(shù)據(jù)庫(kù)連接、判斷以及基于PHP+MariaDB的用戶登陸、管理、刪除等相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05php實(shí)現(xiàn)的rc4加密解密類定義與用法示例
這篇文章主要介紹了php實(shí)現(xiàn)的rc4加密解密類定義與用法,結(jié)合完整實(shí)例形式給出了php rc4加密解密類文件class.rc4crypt.php的定義及相關(guān)使用技巧,需要的朋友可以參考下2018-08-08