生成具有概率机会的随机数

时间:2012-08-31 15:19:09

标签: php random probability

我正在尝试创建一个随机数生成器,它将根据其不断变化的动态概率生成数字。基本上它的功能是它将从页面中选择观看次数最多的帖子到索引页面并显示它。因此,查看次数最多的页面有其计数器。在索引页面上,我使用此计数器,并希望根据将充当概率的计数器生成随机数。越高反击机会越大。到目前为止,我的功能看起来像这样,但我想知道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只是生成数字的正常范围,不会有机会。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这可能会使扳手投入工作并完全改变您打算如何实现这一点。但如果我理解你想做什么,你能不能使用累积概率。

这是我在键盘中敲响的一个例子。显然我的例子很简单,可以编码得更好,但它应该解释一切的机制。基本上,您可以根据视图计数获得每个“帖子”的累积概率,然后生成一个随机数。查看输出中的最后一个数组,您可以看到每个帖子都有可能被显示(或者无论你用它做什么),但是查看得越多的帖子的可能性就越大。

http://codepad.org/9a72bezg

并且代码只是因为它的代码板才会失效。

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

?>
相关问题