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.
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.