php機率性隨機抽獎案例

來源:互聯網
上載者:User
本篇文章主要介紹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的openssl加密擴充使用總結

基於php實現301重新導向跳轉執行個體詳解

PHP正則中的擷取的群組與非擷取的群組執行個體分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.