概率横幅旋转广告

发布于 2024-08-31 00:24:48 字数 306 浏览 10 评论 0原文

我有带有浏览量的横幅广告,例如 CPM 系统。 例如:

i have 3 banner:
banner1 with 20.000 nr of views
banner2 with 10.000 nr of views
banner3 with 5.000 nr of views

在我的网站上,横幅必须出现在这个位置(当页面重新加载时):

banner1banner2banner1banner2banner3

如果浏览量较高,则幽灵出现的概率较高,

我如何在php中执行此操作?

I have banners advertising with number of views, like CPM system.
And for example :

i have 3 banner:
banner1 with 20.000 nr of views
banner2 with 10.000 nr of views
banner3 with 5.000 nr of views

and on my website the banner must to appear in this position (when the page is reloaded) :

banner1 banner2 banner1 banner2 banner3

if the number of views is higher then the probability of apparition is higher

how can i do this in php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红颜悴 2024-09-07 00:24:48

首先,你的系统只是......愚蠢。它使具有大量视图的横幅永久存在,而新创建的具有 0 或很少视图的横幅永远不会有机会被选中,因此永远不会真正被看到...

话虽这么说,如果您有一个数组看起来像这样:

$banners = array
(
    'banner1' => 1,
    'banner2' => 2,
    'banner3' => 4,
    'banner4' => 8,
    'banner5' => 16,
);

您可以使用这样的函数来重点选择一个横幅:

function Probability($data)
{
    if (is_array($data) === true) {
        $result = 0;
        $probability = mt_rand(1, array_sum($data));

        foreach ($data as $key => $value) {
            $result += $value;

            if ($result >= $probability) {
                return $key;
            }
        }
    }

    return false;
}

用法(测试一下@ CodePad .org@ IDEOne):

echo Probability($banners); // banner5

来自 100 次执行的示例:

Array
(
    [banner5] => 41
    [banner4] => 38
    [banner3] => 10
    [banner2] => 8
    [banner1] => 3
)

First of all, your system is just... stupid. It perpetuates banners with lots of views while newly created banners with 0 or few views will never get a chance to be picked and thus will never be actually seen...

That being said, if you have an array that looks like this:

$banners = array
(
    'banner1' => 1,
    'banner2' => 2,
    'banner3' => 4,
    'banner4' => 8,
    'banner5' => 16,
);

You can use a function like this one to weightily pick one banner:

function Probability($data)
{
    if (is_array($data) === true) {
        $result = 0;
        $probability = mt_rand(1, array_sum($data));

        foreach ($data as $key => $value) {
            $result += $value;

            if ($result >= $probability) {
                return $key;
            }
        }
    }

    return false;
}

Usage (test it @ CodePad.org or @ IDEOne):

echo Probability($banners); // banner5

Sample from 100 executions:

Array
(
    [banner5] => 41
    [banner4] => 38
    [banner3] => 10
    [banner2] => 8
    [banner1] => 3
)
暖伴 2024-09-07 00:24:48

这是一种 php 方法,

我想象你的数组看起来像这样...

$banners = array(

    array (
        'name' => 'banner1',
        'views' => 20
    ),
    array (
        'name' => 'banner2',
        'views' => 10
    ),
    array (
        'name' => 'banner3',
        'views' => 5
    )
);

这个函数基本上循环遍历横幅,无论横幅有多少视图,其数组索引的许多项目都会添加到数组中。然后随机选择一个。浏览次数越多的项目被选择的机会就越大。

function getWeightedRandom( $array ) {

    $universe_array = array();

    foreach ( $array as $k => $b ) {
        $universe += $b['views'];
        $universe_array = array_pad( $universe_array, $universe, $k );
}

    $rand = mt_rand( 0, count( $universe_array ) -1 );
    return $array[ $universe_array[ $rand ] ];

}


$r = getWeightedRandom($banners);
print_r($r);

一个简单的 mysql 选项是:

select * from banners order by rand() * views desc limit 1

具有更多浏览次数的横幅将有更高的机会成为顶部结果

Here's a php way to do it

I'm imagining your array will look something like this...

$banners = array(

    array (
        'name' => 'banner1',
        'views' => 20
    ),
    array (
        'name' => 'banner2',
        'views' => 10
    ),
    array (
        'name' => 'banner3',
        'views' => 5
    )
);

This function basically loops through the banners and however many views a banner has, that many items of its array index are added to an array. Then a random one is chosen. Items with more views have a better chance of being chosen.

function getWeightedRandom( $array ) {

    $universe_array = array();

    foreach ( $array as $k => $b ) {
        $universe += $b['views'];
        $universe_array = array_pad( $universe_array, $universe, $k );
}

    $rand = mt_rand( 0, count( $universe_array ) -1 );
    return $array[ $universe_array[ $rand ] ];

}


$r = getWeightedRandom($banners);
print_r($r);

A simple mysql option is:

select * from banners order by rand() * views desc limit 1

banners with more views will have a higher chance of being the top result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文