亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng)代碼

 更新時(shí)間:2016年01月02日 15:51:14   投稿:hebedich  
本文給大家分享的是使用php根據(jù)獎(jiǎng)品的權(quán)重來實(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,說明這個(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ǎ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);
}

相關(guān)文章

  • PHP安裝BCMath擴(kuò)展的方法

    PHP安裝BCMath擴(kuò)展的方法

    今天小編就為大家分享一篇關(guān)于PHP安裝BCMath擴(kuò)展的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 在Mac上編譯安裝PHP7的開發(fā)環(huán)境

    在Mac上編譯安裝PHP7的開發(fā)環(huán)境

    這篇文章主要介紹了在Mac上編譯安裝PHP7的開發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • zend framework配置操作數(shù)據(jù)庫實(shí)例分析

    zend framework配置操作數(shù)據(jù)庫實(shí)例分析

    zend framework項(xiàng)目建立后,看了下zend framework配置操作數(shù)據(jù)庫,本文將詳細(xì)介紹,需要了解的朋友可以參考下
    2012-12-12
  • PHP 模擬$_PUT實(shí)現(xiàn)代碼

    PHP 模擬$_PUT實(shí)現(xiàn)代碼

    PHP里有$_GET,$_POST,但是沒有$_PUT,所以如果需要使用它的話,則你不得不自己模擬一下
    2010-03-03
  • PHP+MariaDB數(shù)據(jù)庫操作基本技巧備忘總結(jié)

    PHP+MariaDB數(shù)據(jù)庫操作基本技巧備忘總結(jié)

    這篇文章主要介紹了PHP+MariaDB數(shù)據(jù)庫操作基本技巧,結(jié)合實(shí)例形式總結(jié)分析了PHP+MariaDB數(shù)據(jù)庫連接、判斷以及基于PHP+MariaDB的用戶登陸、管理、刪除等相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-05-05
  • php隨機(jī)顯示指定文件夾下圖片的方法

    php隨機(jī)顯示指定文件夾下圖片的方法

    這篇文章主要介紹了php隨機(jī)顯示指定文件夾下圖片的方法,涉及array_rand隨機(jī)數(shù)組操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • php使用curl抓取qq空間的訪客信息示例

    php使用curl抓取qq空間的訪客信息示例

    這篇文章主要介紹了php使用curl抓取qq空間的訪客信息示例,需要的朋友可以參考下
    2014-02-02
  • 簡述php環(huán)境搭建與配置

    簡述php環(huán)境搭建與配置

    本文主要對php的環(huán)境搭建與配置進(jìn)行步驟介紹。文章中標(biāo)出了重點(diǎn),方便大家閱讀,需要的朋友可以參考下
    2016-12-12
  • smarty表格換行實(shí)例

    smarty表格換行實(shí)例

    這篇文章主要介紹了smarty表格換行的方法,可實(shí)現(xiàn)針對表格的靈活操作,需要的朋友可以參考下
    2014-12-12
  • php實(shí)現(xiàn)的rc4加密解密類定義與用法示例

    php實(shí)現(xiàn)的rc4加密解密類定義與用法示例

    這篇文章主要介紹了php實(shí)現(xiàn)的rc4加密解密類定義與用法,結(jié)合完整實(shí)例形式給出了php rc4加密解密類文件class.rc4crypt.php的定義及相關(guān)使用技巧,需要的朋友可以參考下
    2018-08-08

最新評(píng)論