Skip to content

Commit

Permalink
[drouting] fixed weight based selection
Browse files Browse the repository at this point in the history
if the last element has weight 1, it will never be selected.
This was instroduced with 48c051f.
The fix is to actually follow the initial PR approach, which was correct. My attempt to improve the original PR actually broke stuff there :(
  • Loading branch information
bogdan-iancu committed Mar 29, 2024
1 parent b1ae0ec commit cdd8430
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/drouting/drouting.c
Original file line number Diff line number Diff line change
Expand Up @@ -2982,11 +2982,11 @@ static int weight_based_sort(pgw_list_t *pgwl, int size, unsigned short *idx)
}
if (weight_sum) {
/* randomly select number */
rand_no = (unsigned int)(weight_sum*((double)rand()/((double)RAND_MAX)));
rand_no = (unsigned int)(weight_sum*((double)rand()/((double)1+RAND_MAX)));
LM_DBG("random number is %d\n",rand_no);
/* select the element */
for( i=first ; i<size ; i++ )
if (running_sum[i]>=rand_no) break;
if (running_sum[i]>rand_no) break;
if (i==size) {
LM_CRIT("bug in weight sort, first=%u, size=%u, rand_no=%u, total weight=%u\n",
first, size, rand_no, weight_sum);
Expand Down

0 comments on commit cdd8430

Please sign in to comment.