国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現概率性隨機抽獎代碼

2024-05-04 23:41:48
字體:
來源:轉載
供稿:網友
本文給大家分享的是使用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/** * 獎品 */class Prize {  # ID  public $id = null;  # 權重  public $weight = null;  # 獎品名  public $name = null;   # 權重范圍區間起始值  protected $start = 0;  # 權重范圍區間結束值  protected $end = 0;   public function __construct($id, $weight, $name) {    if (!$id) {      throw new Exception('獎品ID為空.');    }    $this->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 <= $this->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() <= 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($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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河源市| 南乐县| 遂溪县| 舒兰市| 大安市| 乃东县| 揭阳市| 瓦房店市| 新乐市| 泽库县| 固阳县| 宁城县| 荥阳市| 石嘴山市| 扎兰屯市| 海宁市| 永兴县| 祁东县| 奎屯市| 北碚区| 大田县| 长泰县| 巴里| 鄂托克旗| 喀什市| 厦门市| 清远市| 南岸区| 克山县| 上林县| 宁德市| 报价| 瑞昌市| 日土县| 井研县| 荆州市| 乌拉特中旗| 常宁市| 德阳市| 伊春市| 乐安县|