I have created a function which picks up a unique pool_id on a random basis:
function pick_random_pool($res)
{
// getting random number between 0 and 1 with 9 decimals
$rand = mt_rand(0, 1000000000) / 1000000000;
$pick = 0;
$pool_id = 0;
for ($ii = 0; $ii < count($res); $ii++) {
// if a randomly generated number is bigger keep updating picked id to the last one
if ($rand > $res[$ii]->probability) {
$pick = $res[$ii]->probability;
$pool_id = $res[$ii]->ip_pools_probability_id;
// if a randomly generated number reaches a number more than itself, it stops
} else if ($rand < $res[$ii]->probability) {
// now we need to figure out which of two candidate numbers is the closest to a randomly generated number
if (($res[$ii]->probability - $rand) < ($rand - $pick)) {
$pool_id = $res[$ii]->ip_pools_probability_id;
}
break;
// if by some chance a randomly generated number is exactly the same as a pool's probability
} else {
$pool_id = $res[$ii]->ip_pools_probability_id;
break;
}
}
return $pool_id
}
It looks like it works fine however there is a serious issue. It picks up the biggest number from $res object approx. 90% of the time. For instance, for an array:
Array
(
[0] => stdClass Object
(
[ip_pools_probability_id] => 301
[ip_pools_id] => 12
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.007166877
)
[1] => stdClass Object
(
[ip_pools_probability_id] => 325
[ip_pools_id] => 13
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.008621843
)
[2] => stdClass Object
(
[ip_pools_probability_id] => 349
[ip_pools_id] => 14
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.008984716
)
[3] => stdClass Object
(
[ip_pools_probability_id] => 373
[ip_pools_id] => 15
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.009268012
)
[4] => stdClass Object
(
[ip_pools_probability_id] => 397
[ip_pools_id] => 16
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.010412571
)
...
[20] => stdClass Object
(
[ip_pools_probability_id] => 61
[ip_pools_id] => 2
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.094612022
)
[21] => stdClass Object
(
[ip_pools_probability_id] => 85
[ip_pools_id] => 3
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.097033897
)
[22] => stdClass Object
(
[ip_pools_probability_id] => 133
[ip_pools_id] => 5
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.098855823
)
[23] => stdClass Object
(
[ip_pools_probability_id] => 109
[ip_pools_id] => 4
[time_start] => 12:00:00
[time_end] => 13:00:00
[probability] => 0.099785568
)
)
it will pick up the 23rd element approx. 90% of the time. Why it is not evenly distributed? Where is the flaw in the code?
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire