Random numbers….where all numbers are equal but some more equal than others…

For the ph_lyrics project the tweets occur at random intervals of time. But it was desired that the chosen interval should be more likely to be a longer rather than shorter interval. What was needed was a php mechanism for generating weighted random numbers.

As ever the folks over at stackoverflow had the answer.

Which looks like this:

function weightedrand($min, $max, $gamma) {
$offset= $max-$min+1;
return floor($min+pow(lcg_value(), $gamma)*$offset);
}
 
This entry was posted in PHP and tagged , , . Bookmark the permalink.

Comments are closed.