Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5382203a
Commit
5382203a
authored
Feb 16, 2019
by
Raymond Hettinger
Committed by
GitHub
Feb 16, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert range to repeat for choices() (#11889)
parent
38f11cc3
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
3 additions
and
3 deletions
+3
-3
Lib/random.py
Lib/random.py
+3
-3
No files found.
Lib/random.py
View file @
5382203a
...
@@ -43,7 +43,7 @@ from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
...
@@ -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
os
import
urandom
as
_urandom
from
_collections_abc
import
Set
as
_Set
,
Sequence
as
_Sequence
from
_collections_abc
import
Set
as
_Set
,
Sequence
as
_Sequence
from
hashlib
import
sha512
as
_sha512
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
from
bisect
import
bisect
as
_bisect
import
os
as
_os
import
os
as
_os
...
@@ -389,7 +389,7 @@ class Random(_random.Random):
...
@@ -389,7 +389,7 @@ class Random(_random.Random):
if
weights
is
None
:
if
weights
is
None
:
_int
=
int
_int
=
int
n
+=
0.0
# convert to float for a small speed improvement
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
))
cum_weights
=
list
(
_accumulate
(
weights
))
elif
weights
is
not
None
:
elif
weights
is
not
None
:
raise
TypeError
(
'Cannot specify both weights and cumulative weights'
)
raise
TypeError
(
'Cannot specify both weights and cumulative weights'
)
...
@@ -399,7 +399,7 @@ class Random(_random.Random):
...
@@ -399,7 +399,7 @@ class Random(_random.Random):
total
=
cum_weights
[
-
1
]
+
0.0
# convert to float
total
=
cum_weights
[
-
1
]
+
0.0
# convert to float
hi
=
n
-
1
hi
=
n
-
1
return
[
population
[
bisect
(
cum_weights
,
random
()
*
total
,
0
,
hi
)]
return
[
population
[
bisect
(
cum_weights
,
random
()
*
total
,
0
,
hi
)]
for
i
in
range
(
k
)]
for
i
in
_repeat
(
None
,
k
)]
## -------------------- real-valued distributions -------------------
## -------------------- real-valued distributions -------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment