|
@@ -100,8 +100,12 @@ class Consensus:
|
|
|
def select_weighted_relay(self, cdf):
|
|
|
"""Use the cdf generated by bw_cdf to select a relay with
|
|
|
probability proportional to its bw weight."""
|
|
|
- val = random.randint(1, self.consdict['totbw'])
|
|
|
- idx = bisect.bisect_left(cdf, val)
|
|
|
+ totbw = self.consdict['totbw']
|
|
|
+ if totbw < 1:
|
|
|
+ raise ValueError("No relays to choose from")
|
|
|
+ val = random.randint(0, totbw-1)
|
|
|
+ # Find the rightmost entry less than or equal to val
|
|
|
+ idx = bisect.bisect_right(cdf, val)
|
|
|
return self.consdict['relays'][idx-1]
|
|
|
|
|
|
|