搜尋
首頁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在某个奖品的权重范围值内,标识这个奖品被抽取到


1. [代码][PHP]代码

<?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(&#39;奖品ID为空.&#39;);
		}
		$this->id = $id;
		$this->weight = $weight ? $weight : 0;
		$this->name = $name ? $name : &#39;随机奖品&#39; . $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(&#39;奖池未初始化&#39;);
		}

		if ($poll->count() <= 0) {
			throw new Exception(&#39;奖池为空&#39;);
		}

		$items = $poll->getVisibleItems();
		if (count($items) <= 0) {
			throw new Exception(&#39;奖池为空&#39;);
		}

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

                   

                   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具