Giorgi
Giorgi

Reputation: 609

Generate random number with probability chance

i'm trying to create a random number generator that will generate number based on its dynamic probability that keeps changing. Basically the functionality of it is that it will select the most viewed posts from the page to index page and show it. So the page that is most viewed has its counter. On index page i take this counter and want to generate a random number based on the counter that will act as a probability. The higher counter the better chance. My function so far looks like this but i want to know if there is any built in function in PHP or how to make this better and efficient:

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 is just the normal range to generate the number which does not fall in the chance.

Thank you for your help.

Upvotes: 1

Views: 6507

Answers (1)

Lee
Lee

Reputation: 10603

This might be throwing a spanner in the works and completely changing how you intend to implement this. But if i understand correctly what you want to do, could you not use cumulative probability.

Here's an example i knocked up in codepad. Obviously my example is simple and could be coded better but it should explain the mechanics of it all. Basically you get the cumulative probability of each "post" based on its view count, then generate a random number. Look at the last array in the output and you can see each post has a chance of being displayed (or whatever you do with it) but the higher viewed ones will have a greater probability.

http://codepad.org/9a72bezg

And heres the code just for the sake of it codepad ever goes down.

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

?>

Upvotes: 1

Related Questions