我正在构建一个广告横幅轮播脚本基于印象,它可以在整个月内均匀地展示广告。每次请求显示广告时都会进行计算。所以这将在运行中完成。广告应该轮流展示,一个接一个,而不是只展示一个广告获得 1000 次展示,然后展示另一个广告获得 1000 次展示。它在大多数情况下应该显示 1 次展示,然后切换广告(当然除非一个广告比另一个广告有更多的展示要用完)。
假设我有 5 个广告,每个广告都有不同数量的购买展示次数,公式是什么/您如何转换广告?我希望在PHP.
广告 #1:1,000 次购买展示
广告 #2:12,000 次购买展示
广告 #3:3,000 次购买展示
广告 #4:20,000 次购买展示
广告 #5:10,000 次购买展示
如果有多个广告都在同一时间范围内购买了 1000 次展示,则它应该一个接一个地展示,直到展示次数用完。不过,我认为如果一个人在短时间内购买了 1000 次展示可能会很好,我应该考虑到这一点并以更快的速度展示它们。我乐于接受建议。
最佳答案
我认为你应该使用最好的算法来完成最好的工作,我只会向你展示一些如何实现这种算法的可能性
我当前的示例将显示使用
- shuffle
- fisherYatesShuffle
- 罗宾洗牌
- 比率随机播放
你也可以实现
- 基于修道院的洗牌
- 时基随机播放
- 百分比
- 点击随机播放
- 等等
简单的概念证明
// Create Add Infroamtion
$ads = array();
$ads[] = new Ad(10, "A.jpg", 2);
$ads[] = new Ad(12, "B.gif", 3);
$ads[] = new Ad(30, "C.png", 7);
$ads[] = new Ad(20, "D.swf", 5);
// Add ads to banner
$banner = new Banner($ads);
// You can also add addional ads
$banner->add(new Ad(10, "E.swf"));
echo "<pre>";
//Lets Emulate first 100 rotations
for($i = 0; $i < 1000; $i ++) {
// Select Algorithm
$banner->randomise("ratioShuffle");
// Display Add
echo $banner->getDisplay(), PHP_EOL;
}
可以使用的简单随机播放功能
function fisherYatesShuffle(array &$items) {
for($i = count($items) - 1; $i > 0; $i --) {
$j = @mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
}
function robinShuffle(array &$items) {
usort($items, function ($a, $b) {
$a = $a->getDisplay();
$b = $b->getDisplay();
return $a == $b ? 0 : ($a < $b ? - 1 : 1);
});
}
function ratioShuffle(array &$items) {
static $called = false;
if ($called === false) {
$ads = array();
foreach ( $items as &$ad ) {
for($i = 0; $i < $ad->getRatio(); $i ++) {
$ads[] = $ad;
}
}
$called = true;
$items = $ads;
}
shuffle($items);
}
使用的类
class Ad implements JsonSerializable {
private $impressions;
private $media;
private $ratio = 1;
private $display = 0;
function __construct($impressions, $media = null, $ratio = 1) {
$this->impressions = $impressions;
$this->media = $media;
$this->ratio = $ratio;
}
function torch() {
$this->impressions --;
$this->display ++;
}
public function getImpression() {
return $this->impressions;
}
public function getDisplay() {
return $this->display;
}
public function getRatio() {
return $this->ratio;
}
public function getMeadia() {
return $this->media;
}
public function __toString() {
return json_encode($this->jsonSerialize());
}
public function jsonSerialize() {
return get_object_vars($this);
}
}
class Banner implements Countable, JsonSerializable {
private $totalImpressions;
private $ads = array();
function __construct(array $ads) {
foreach ( $ads as $ad )
$this->add($ad);
}
public function add(Ad $ad) {
$this->ads[] = $ad;
$this->totalImpressions += $ad->getImpression();
}
public function randomise($function = null) {
if (is_callable($function, false, $callable_name)) {
return $callable_name($this->ads);
} else {
return shuffle($this->ads);
}
}
public function getDisplay() {
foreach ( $this->ads as &$ad ) {
if ($ad->getImpression() < 1) {
unset($ad);
continue;
}
$ad->torch();
break;
}
return isset($ad) ? $ad : null;
}
public function jsonSerialize() {
$array = $this->ads;
foreach ( $array as &$ad ) {
$ad = $ad->jsonSerialize();
}
return $array;
}
public function __toString() {
return json_encode($this->jsonSerialize());
}
function count() {
return count($this->ads);
}
}
正如您所看到的,这是一个示例....只需尝试使您的解决方案灵活
关于php - 创建横幅交换算法以旋转广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709558/