| 49 | var randInt64n = rand.Int64N |
| 50 | |
| 51 | func (rw *randomWRR) Next() (item any) { |
| 52 | if len(rw.items) == 0 { |
| 53 | return nil |
| 54 | } |
| 55 | if rw.equalWeights { |
| 56 | return rw.items[randInt64n(int64(len(rw.items)))].item |
| 57 | } |
| 58 | |
| 59 | sumOfWeights := rw.items[len(rw.items)-1].accumulatedWeight |
| 60 | // Random number in [0, sumOfWeights). |
| 61 | randomWeight := randInt64n(sumOfWeights) |
| 62 | // Item's accumulated weights are in ascending order, because item's weight >= 0. |
| 63 | // Binary search rw.items to find first item whose accumulatedWeight > randomWeight |
| 64 | // The return i is guaranteed to be in range [0, len(rw.items)) because randomWeight < last item's accumulatedWeight |
| 65 | i := sort.Search(len(rw.items), func(i int) bool { return rw.items[i].accumulatedWeight > randomWeight }) |
| 66 | return rw.items[i].item |
| 67 | } |
| 68 | |
| 69 | func (rw *randomWRR) Add(item any, weight int64) { |
| 70 | accumulatedWeight := weight |