diff options
author | Raymond Hettinger <python@rcn.com> | 2016-10-29 16:57:09 -0700 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-10-29 16:57:09 -0700 |
commit | ba25f6149052aea017df5fa328867d090bb89d1c (patch) | |
tree | a85f1c3e775ef3b1255a021751cb25e943c072e6 /Lib/random.py | |
parent | 7ca671532cee6e8ed80dbd6e461f1de50ee93a3b (diff) | |
parent | 30d00e54dde47b11f5b338aaba17760b641e1705 (diff) | |
download | cpython-git-ba25f6149052aea017df5fa328867d090bb89d1c.tar.gz |
merge
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/Lib/random.py b/Lib/random.py index ef8cb05601..a047444502 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -344,10 +344,12 @@ class Random(_random.Random): the selections are made with equal probability. """ + random = self.random if cum_weights is None: if weights is None: - choice = self.choice - return [choice(population) for i in range(k)] + _int = int + total = len(population) + return [population[_int(random() * total)] for i in range(k)] else: cum_weights = list(_itertools.accumulate(weights)) elif weights is not None: @@ -355,7 +357,6 @@ class Random(_random.Random): if len(cum_weights) != len(population): raise ValueError('The number of weights does not match the population') bisect = _bisect.bisect - random = self.random total = cum_weights[-1] return [population[bisect(cum_weights, random() * total)] for i in range(k)] |