Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
91e27c25
Kaydet (Commit)
91e27c25
authored
Agu 19, 2005
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Implement random.sample() using sets instead of dicts.
üst
e0245143
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
9 deletions
+13
-9
random.py
Lib/random.py
+13
-9
No files found.
Lib/random.py
Dosyayı görüntüle @
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