vendredi 20 septembre 2019

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]; 
}




Aucun commentaire:

Enregistrer un commentaire