最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年01月02日 15:51:14   投稿:hebedich  
本文給大家分享的是使用php根據(jù)獎品的權(quán)重來實(shí)現(xiàn)概率性隨機(jī)抽獎的代碼,非常的使用,有類似需求的小伙伴,可以拿去參考下

1、初始數(shù)據(jù):

權(quán)重越大,抽取的幾率越高
[獎品1, 權(quán)重 5], [ 獎品2, 權(quán)重6], [ 獎品3, 權(quán)重 7], [ 獎品4, 權(quán)重2]

2、處理步驟:

1)N = 5 + 6 + 7 + 2 = 20
2)然后取1-N的隨機(jī)數(shù)M
3)界定各 獎品的權(quán)重范圍值 獎品 1 : 1-5 ; 獎品2 : 6-11; 獎品3: 12-18; 獎品4: 19-20
4) 如果M在某個獎品的權(quán)重范圍值內(nèi),標(biāo)識這個獎品被抽取到

<?php
/**
 * 獎品
 */
class Prize {
  # ID
  public $id = null;
  # 權(quán)重
  public $weight = null;
  # 獎品名
  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('獎品ID為空.');
    }
    $this->id = $id;
    $this->weight = $weight ? $weight : 0;
    $this->name = $name ? $name : '隨機(jī)獎品' . $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);
  }
}
 
/**
 * 獎品池
 */
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;
  }
 
  # 所有所有可用獎品(如果權(quán)重為0,說明這個獎品永遠(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);
  }
}
 
/**
 * 簡單的抽獎類
 */
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;
 
      # 設(shè)置獎品的權(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è)置獎池
  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)文章

最新評論

漳州市| 秭归县| 龙游县| 左云县| 虞城县| 巴东县| 新昌县| 灌南县| 开鲁县| 瑞金市| 天津市| 竹山县| 威信县| 通辽市| 扶风县| 娱乐| 蛟河市| 四川省| 夹江县| 奉节县| 万山特区| 邮箱| 拜城县| 新营市| 麦盖提县| 辛集市| 玛纳斯县| 洛川县| 六盘水市| 龙岩市| 手游| 淮滨县| 鄱阳县| 饶阳县| 合川市| 柳河县| 定安县| 福鼎市| 正安县| 怀安县| 淮阳县|