天天看點

php 機率不等随機,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在某個獎品的權重範圍值内,辨別這個獎品被抽取到

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);

}