]> granicus.if.org Git - python/commitdiff
Convert range to repeat for choices() (#11889)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Sat, 16 Feb 2019 21:30:51 +0000 (13:30 -0800)
committerGitHub <noreply@github.com>
Sat, 16 Feb 2019 21:30:51 +0000 (13:30 -0800)
Lib/random.py

index 754b2952a0f7ddd3484953d66e2561c9fb1aa9b2..79ef30d7d18d174f311af4ef2d12fe3a1f10194d 100644 (file)
@@ -43,7 +43,7 @@ from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
 from os import urandom as _urandom
 from _collections_abc import Set as _Set, Sequence as _Sequence
 from hashlib import sha512 as _sha512
-from itertools import accumulate as _accumulate
+from itertools import accumulate as _accumulate, repeat as _repeat
 from bisect import bisect as _bisect
 import os as _os
 
@@ -389,7 +389,7 @@ class Random(_random.Random):
             if weights is None:
                 _int = int
                 n += 0.0    # convert to float for a small speed improvement
-                return [population[_int(random() * n)] for i in range(k)]
+                return [population[_int(random() * n)] for i in _repeat(None, k)]
             cum_weights = list(_accumulate(weights))
         elif weights is not None:
             raise TypeError('Cannot specify both weights and cumulative weights')
@@ -399,7 +399,7 @@ class Random(_random.Random):
         total = cum_weights[-1] + 0.0   # convert to float
         hi = n - 1
         return [population[bisect(cum_weights, random() * total, 0, hi)]
-                for i in range(k)]
+                for i in _repeat(None, k)]
 
 ## -------------------- real-valued distributions  -------------------