mirror of
https://github.com/python/cpython.git
synced 2025-08-23 18:24:46 +00:00
Issue #18844: Make the various ways for specifing weights produce the same results.
This commit is contained in:
parent
ab5cf4da4e
commit
30d00e54dd
3 changed files with 23 additions and 3 deletions
|
@ -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)]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue