From a3b1b9087429877b608976482c54e53f819a5e0d Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Fri, 6 Oct 2017 02:49:23 +0200 Subject: [PATCH] Added actual weighted choice. Numpy for now, Python 3.6 has it in random.choices(). --- tcllib.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tcllib.py b/tcllib.py index d1e9513..ed67a6e 100644 --- a/tcllib.py +++ b/tcllib.py @@ -5,6 +5,7 @@ import base64 import binascii import hashlib +import numpy import platform import random import time @@ -89,7 +90,13 @@ class FotaCheck: return "{}{}".format(str(millis), tail) def get_master_server(self): - return random.choice(self.master_servers) + weight_sum = 0 + for i in self.master_servers_weights: + weight_sum += i + numpy_weights = [] + for i in self.master_servers_weights: + numpy_weights.append(i/weight_sum) + return numpy.random.choice(self.master_servers, p=numpy_weights) def master_server_downvote(self): idx = self.master_servers.index(self.g2master)