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
30d00e54
Commit
30d00e54
authored
Oct 29, 2016
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #18844: Make the various ways for specifing weights produce the same results.
parent
ab5cf4da
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
3 deletions
+23
-3
Lib/random.py
Lib/random.py
+4
-3
Lib/test/test_random.py
Lib/test/test_random.py
+16
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/random.py
View file @
30d00e54
...
...
@@ -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
)]
...
...
Lib/test/test_random.py
View file @
30d00e54
...
...
@@ -629,6 +629,22 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
self
.
assertTrue
(
stop
<
x
<=
start
)
self
.
assertEqual
((
x
+
stop
)
%
step
,
0
)
def
test_choices_algorithms
(
self
):
# The various ways of specifing weights should produce the same results
choices
=
self
.
gen
.
choices
n
=
13132817
self
.
gen
.
seed
(
8675309
)
a
=
self
.
gen
.
choices
(
range
(
n
),
k
=
10000
)
self
.
gen
.
seed
(
8675309
)
b
=
self
.
gen
.
choices
(
range
(
n
),
[
1
]
*
n
,
k
=
10000
)
self
.
assertEqual
(
a
,
b
)
self
.
gen
.
seed
(
8675309
)
c
=
self
.
gen
.
choices
(
range
(
n
),
cum_weights
=
range
(
1
,
n
+
1
),
k
=
10000
)
self
.
assertEqual
(
a
,
c
)
def
gamma
(
z
,
sqrt2pi
=
(
2.0
*
pi
)
**
0.5
):
# Reflection to right half of complex plane
if
z
<
0.5
:
...
...
Misc/NEWS
View file @
30d00e54
...
...
@@ -34,6 +34,9 @@ Library
- Issue #27275: Fixed implementation of pop() and popitem() methods in
subclasses of accelerated OrderedDict.
- Issue #18844: The various ways of specifing weights for random.choices()
now produce the same result sequences.
- Issue #28255: calendar.TextCalendar().prmonth() no longer prints a space
at the start of new line after printing a month'
s
calendar
.
Patch
by
Xiang
Zhang
.
...
...
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