[GH-ISSUE #3] If records have weight, choose randomly based on weight #148

Closed
opened 2026-03-01 17:24:12 +03:00 by kerem · 0 comments
Owner

Originally created by @abh on GitHub (Aug 24, 2012).
Original GitHub issue: https://github.com/abh/geodns/issues/3

If records have weight configured, choose them randomly based on the weight. The perl implementation sorts the records by weight (descending), adds up the total weight and then generates a random number between 0 and then loops through the records until the accumulated weight is higher than the random number.

Stackoverflow suggests basically the same algorithm, though modifying the slice after each pick to avoid duplicates (the perl version tries doing that with brute-force).

Originally created by @abh on GitHub (Aug 24, 2012). Original GitHub issue: https://github.com/abh/geodns/issues/3 If records have weight configured, choose them randomly based on the weight. The [perl implementation](https://github.com/abh/pgeodns/blob/master/lib/GeoDNS.pm#L270) sorts the records by weight (descending), adds up the total weight and then generates a random number between 0 and then loops through the records until the accumulated weight is higher than the random number. [Stackoverflow](http://stackoverflow.com/questions/352670/weighted-random-selection-with-and-without-replacement) suggests basically the same algorithm, though modifying the slice after each pick to avoid duplicates (the perl version tries doing that with brute-force).
kerem closed this issue 2026-03-01 17:24:12 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/geodns#148
No description provided.