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
91e27c25
Commit
91e27c25
authored
Aug 19, 2005
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement random.sample() using sets instead of dicts.
parent
e0245143
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
9 deletions
+13
-9
Lib/random.py
Lib/random.py
+13
-9
No files found.
Lib/random.py
View file @
91e27c25
...
@@ -41,7 +41,7 @@ General notes on the underlying Mersenne Twister core generator:
...
@@ -41,7 +41,7 @@ General notes on the underlying Mersenne Twister core generator:
from
warnings
import
warn
as
_warn
from
warnings
import
warn
as
_warn
from
types
import
MethodType
as
_MethodType
,
BuiltinMethodType
as
_BuiltinMethodType
from
types
import
MethodType
as
_MethodType
,
BuiltinMethodType
as
_BuiltinMethodType
from
math
import
log
as
_log
,
exp
as
_exp
,
pi
as
_pi
,
e
as
_e
from
math
import
log
as
_log
,
exp
as
_exp
,
pi
as
_pi
,
e
as
_e
,
ceil
as
_ceil
from
math
import
sqrt
as
_sqrt
,
acos
as
_acos
,
cos
as
_cos
,
sin
as
_sin
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
binascii
import
hexlify
as
_hexlify
from
binascii
import
hexlify
as
_hexlify
...
@@ -286,15 +286,14 @@ class Random(_random.Random):
...
@@ -286,15 +286,14 @@ class Random(_random.Random):
"""
"""
# Sampling without replacement entails tracking either potential
# Sampling without replacement entails tracking either potential
# selections (the pool) in a list or previous selections in a
# selections (the pool) in a list or previous selections in a set.
# dictionary.
# When the number of selections is small compared to the
# When the number of selections is small compared to the
# population, then tracking selections is efficient, requiring
# population, then tracking selections is efficient, requiring
# only a small
dictionary
and an occasional reselection. For
# only a small
set
and an occasional reselection. For
# a larger number of selections, the pool tracking method is
# a larger number of selections, the pool tracking method is
# preferred since the list takes less space than the
# preferred since the list takes less space than the
#
dictionary
and it doesn't suffer from frequent reselections.
#
set
and it doesn't suffer from frequent reselections.
n
=
len
(
population
)
n
=
len
(
population
)
if
not
0
<=
k
<=
n
:
if
not
0
<=
k
<=
n
:
...
@@ -302,7 +301,10 @@ class Random(_random.Random):
...
@@ -302,7 +301,10 @@ class Random(_random.Random):
random
=
self
.
random
random
=
self
.
random
_int
=
int
_int
=
int
result
=
[
None
]
*
k
result
=
[
None
]
*
k
if
n
<
6
*
k
:
# if n len list takes less space than a k len dict
setsize
=
21
# size of a small set minus size of an empty list
if
k
>
5
:
setsize
+=
4
**
_ceil
(
_log
(
k
*
3
,
4
))
# table size for big sets
if
n
<=
setsize
:
# is an n-length list smaller than a k-length set
pool
=
list
(
population
)
pool
=
list
(
population
)
for
i
in
xrange
(
k
):
# invariant: non-selected at [0,n-i)
for
i
in
xrange
(
k
):
# invariant: non-selected at [0,n-i)
j
=
_int
(
random
()
*
(
n
-
i
))
j
=
_int
(
random
()
*
(
n
-
i
))
...
@@ -311,14 +313,16 @@ class Random(_random.Random):
...
@@ -311,14 +313,16 @@ class Random(_random.Random):
else
:
else
:
try
:
try
:
n
>
0
and
(
population
[
0
],
population
[
n
//
2
],
population
[
n
-
1
])
n
>
0
and
(
population
[
0
],
population
[
n
//
2
],
population
[
n
-
1
])
except
(
TypeError
,
KeyError
):
# handle
sets and dictionari
es
except
(
TypeError
,
KeyError
):
# handle
non-sequence iterabl
es
population
=
tuple
(
population
)
population
=
tuple
(
population
)
selected
=
{}
selected
=
set
()
selected_add
=
selected
.
add
for
i
in
xrange
(
k
):
for
i
in
xrange
(
k
):
j
=
_int
(
random
()
*
n
)
j
=
_int
(
random
()
*
n
)
while
j
in
selected
:
while
j
in
selected
:
j
=
_int
(
random
()
*
n
)
j
=
_int
(
random
()
*
n
)
result
[
i
]
=
selected
[
j
]
=
population
[
j
]
selected_add
(
j
)
result
[
i
]
=
population
[
j
]
return
result
return
result
## -------------------- 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