Php 利用概率机会生成随机数

Php 利用概率机会生成随机数,php,random,probability,Php,Random,Probability,我试图创建一个随机数生成器,它将根据不断变化的动态概率生成数字。基本上,它的功能是,它将选择从页面到索引页面浏览次数最多的帖子,并显示出来。因此,浏览量最大的页面有其计数器。在索引页上,我使用这个计数器,并希望根据计数器生成一个随机数,作为概率。计数器越高,机会越大。到目前为止,我的函数看起来像这样,但我想知道PHP中是否有内置函数,或者如何使其更好、更高效: private function randomWithProbability($chance, $num, $range = false

我试图创建一个随机数生成器,它将根据不断变化的动态概率生成数字。基本上,它的功能是,它将选择从页面到索引页面浏览次数最多的帖子,并显示出来。因此,浏览量最大的页面有其计数器。在索引页上,我使用这个计数器,并希望根据计数器生成一个随机数,作为概率。计数器越高,机会越大。到目前为止,我的函数看起来像这样,但我想知道PHP中是否有内置函数,或者如何使其更好、更高效:

private function randomWithProbability($chance, $num, $range = false)
 {
    /* first generate a number from 1 to 100 and see if that number is in the range of chance */
    $rand = mt_rand(1, 100);

    if ($rand <= $chance) 
    {
        /* the number should be returned */
        return $num;
    }
    else 
    {
        /* otherwise return a random number */
        if ($range !== false)
        {
            /* make sure that this number is not same as the number for which we specified the chance */
            $rand = mt_rand(0, $range-1);
            while ($rand == $num)
            {
                $rand = mt_rand(0, $range-1);
            }

            return $rand;
        }
    }
 }
$range只是生成不属于偶然数的数字的正常范围


谢谢你的帮助。

这可能是在工作中使用扳手,完全改变了你打算如何实现这一点。但如果我正确理解了你想做什么,你能不能不使用累积概率

这是我在代码板中找到的一个例子。显然,我的示例很简单,可以编写得更好,但它应该解释所有的机制。基本上,您可以根据每个帖子的浏览次数来获得其累积概率,然后生成一个随机数。查看输出中的最后一个数组,您可以看到每个帖子都有被显示的机会,或者您可以对其执行任何操作,但是查看次数越高的帖子的可能性越大

这里的代码只是为了它的缘故,代码板一直在下降

<?php
$posts = array(
 "php article" => 100,
 "c++ article" => 5,
 "monkey article" => 2,
 "another article" => 67,
 "one more" => 87,
 "ok i wanted another" => 34,
 "last one i promise" => 21
);

// Get total views
$total_views = array_sum($posts);

echo "total views: ".$total_views."\n\n";

// percentage posts
$posts_percents = array();
foreach($posts as $k => $v) {
  $posts_percents[$k] = ($v / $total_views) * 100;
}


//sort um
asort($posts_percents);

echo "chances of each post being viewed";
print_r($posts_percents);

//--- work out cumulative percents
$cumulative = array();
foreach($posts_percents as $k => $v) {
    $cumulative[$k] = end($cumulative) + $v;

}

echo "the cumulative probability";
print_r($cumulative);

//--- just a function i will use in the loop below, hopefully being in a function makes it easier to read than if i put it directly in th loop.
function get_post() {
   global $cumulative;

   asort($cumulative);

   $lowest = floor(current($cumulative));

   $rand = mt_rand($lowest,100);
   foreach($cumulative as $k => $v) {
     if($v > $rand)  {
      // echo $k." (".$rand.")\n";
       return $k;
       }
   }

}


$views=array();
for($i=0;$i<=1000;$i++) {
  $post_viewed = get_post();

  if(empty($post_viewed))
    continue;

  if(isset($views[$post_viewed]))
     $views[$post_viewed]++;
  else
     $views[$post_viewed]=1;

}

arsort($views);

echo "\n chances of being displayed \n\n";
print_r($views);

?>

这可能是在工作中使用扳手,完全改变了您打算如何实现这一点。但如果我正确理解了你想做什么,你能不能不使用累积概率

这是我在代码板中找到的一个例子。显然,我的示例很简单,可以编写得更好,但它应该解释所有的机制。基本上,您可以根据每个帖子的浏览次数来获得其累积概率,然后生成一个随机数。查看输出中的最后一个数组,您可以看到每个帖子都有被显示的机会,或者您可以对其执行任何操作,但是查看次数越高的帖子的可能性越大

这里的代码只是为了它的缘故,代码板一直在下降

<?php
$posts = array(
 "php article" => 100,
 "c++ article" => 5,
 "monkey article" => 2,
 "another article" => 67,
 "one more" => 87,
 "ok i wanted another" => 34,
 "last one i promise" => 21
);

// Get total views
$total_views = array_sum($posts);

echo "total views: ".$total_views."\n\n";

// percentage posts
$posts_percents = array();
foreach($posts as $k => $v) {
  $posts_percents[$k] = ($v / $total_views) * 100;
}


//sort um
asort($posts_percents);

echo "chances of each post being viewed";
print_r($posts_percents);

//--- work out cumulative percents
$cumulative = array();
foreach($posts_percents as $k => $v) {
    $cumulative[$k] = end($cumulative) + $v;

}

echo "the cumulative probability";
print_r($cumulative);

//--- just a function i will use in the loop below, hopefully being in a function makes it easier to read than if i put it directly in th loop.
function get_post() {
   global $cumulative;

   asort($cumulative);

   $lowest = floor(current($cumulative));

   $rand = mt_rand($lowest,100);
   foreach($cumulative as $k => $v) {
     if($v > $rand)  {
      // echo $k." (".$rand.")\n";
       return $k;
       }
   }

}


$views=array();
for($i=0;$i<=1000;$i++) {
  $post_viewed = get_post();

  if(empty($post_viewed))
    continue;

  if(isset($views[$post_viewed]))
     $views[$post_viewed]++;
  else
     $views[$post_viewed]=1;

}

arsort($views);

echo "\n chances of being displayed \n\n";
print_r($views);

?>

我会在…时使用do{…}。。。循环以除去一行无关的代码。除此之外,它看起来不错。1==mt_rand1100意味着百分之一的概率。1==mt_rand1,10000意味着1/10000的概率,所以如果我想要生成数字2的50%概率,我会做2==mt_rand1,50?@user16。。。不,你会做2==mt_rand1,2。这个条件有50%的可能性为真。@drrcknlsn谢谢你,我将它改为do{},而我会在。。。循环以除去一行无关的代码。除此之外,它看起来不错。1==mt_rand1100意味着百分之一的概率。1==mt_rand1,10000意味着1/10000的概率,所以如果我想要生成数字2的50%概率,我会做2==mt_rand1,50?@user16。。。不,你会做2==mt_rand1,2。这个条件有50%的可能性是真的。@drrcknlsn谢谢你,我把它改成了{},谢谢你,这是有道理的,但这只用于帖子,如果我想重复使用算法并显示随机页面,那该怎么办呢?它基本上需要一个帖子/页面/内容列表和查看计数器。你可以将算法封装在一个函数中,通过这个函数,你可以传递一组内容,让它返回其中一个条目或所选条目的一个键。谢谢,这是有意义的,但这将只用于帖子。如果我想重用算法并显示随机页面,那该怎么办?它基本上需要一个帖子/页面/内容列表和查看计数器。您可以将algo封装在一个函数中,通过该函数传递一个填充数组,让它返回一个条目或所选条目的键。