简体   繁体   中英

Weighted Random Choice In PHP

I need help getting a my probability odds closer to testing results with low percentages. What I have seems to work for percentages at 1% or higher but I need it to work with very low percentages such as 0.02% (down to 4 decimals). Anything below 1% tends to end up having around a 1% probability after running tests from running 1000-100000 tests at once the results are similar.

Example Results

ID  Odds    Test Total  Test Odds
1   60.0000 301773  60.3546%
2   30.0000 148360  29.672%
3   9.9800  44897   8.9794%
4   0.0200  4970    0.994%

Function

// $values = [1,2,3,4]
// $weights = [60.0000,30.0000,9.9800,.0200]
private function getRandom($values, $weights)
{
    $count = count($values); 
    $i = 0; 
    $n = 0; 
    $num = mt_rand(0, array_sum($weights)); 
    while($i < $count)
    {
        $n += $weights[$i]; 
        if($n >= $num)
            break; 
        $i++; 
    } 
    return $values[$i]; 
}

mt_rand returns an integer so comparing it to 0.02 is effectively the same as comparing it to 1. Hence you always get around 1% for the weights which are less than 1%. Try computing $num like this instead:

$num = mt_rand(0, array_sum($weights) * 100) / 100; 

Demo on 3v4l.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM