Skip to content

Commit

Permalink
Update drouting.c
Browse files Browse the repository at this point in the history
In weight_based_sort() function:

- Changed RAND_MAX to RAND_MAX + 1 to make sure that quotient is always less than 1
- Changed float to double to avoid rounding errors, particularly when the value of rand() nears RAND_MAX and thus the quotient could round to 1

(cherry picked from commit 6ef8168)
  • Loading branch information
nexbridge authored and bogdan-iancu committed Mar 16, 2023
1 parent f1e37b4 commit 65f4225
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 @@ -2878,7 +2878,7 @@ 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*((float)rand()/(float)RAND_MAX));
rand_no = (unsigned int)(weight_sum*((double)rand()/((double)RAND_MAX+1)));
LM_DBG("random number is %d\n",rand_no);
/* select the element */
for( i=first ; i<size ; i++ )
Expand All @@ -2895,7 +2895,7 @@ static int weight_based_sort(pgw_list_t *pgwl, int size, unsigned short *idx)
}
} else {
/* randomly select index */
// i = (unsigned int)((size-first)*((float)rand()/RAND_MAX));
// i = (unsigned int)((size-first)*((double)rand()/((double)RAND_MAX+1)));
i = first;
}
LM_DBG("selecting element %d with weight %d\n",
Expand Down

0 comments on commit 65f4225

Please sign in to comment.