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
e4a3e999
Kaydet (Commit)
e4a3e999
authored
Eyl 08, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
In the case where only a user supplied random() method is available,
adopt a strategy that makes the fewest calls to random().
üst
51e01a6f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
12 deletions
+12
-12
random.py
Lib/random.py
+12
-12
No files found.
Lib/random.py
Dosyayı görüntüle @
e4a3e999
...
@@ -212,33 +212,33 @@ class Random(_random.Random):
...
@@ -212,33 +212,33 @@ class Random(_random.Random):
return
self
.
randrange
(
a
,
b
+
1
)
return
self
.
randrange
(
a
,
b
+
1
)
def
_randbelow
(
self
,
n
,
int
=
int
,
bpf
=
BPF
,
type
=
type
,
def
_randbelow
(
self
,
n
,
int
=
int
,
maxsize
=
1
<<
BPF
,
type
=
type
,
Method
=
_MethodType
,
BuiltinMethod
=
_BuiltinMethodType
):
Method
=
_MethodType
,
BuiltinMethod
=
_BuiltinMethodType
):
"""Return a random int in the range [0,n). Raises ValueError if n==0.
"Return a random int in the range [0,n). Raises ValueError if n==0."
"""
k
=
n
.
bit_length
()
# don't use (n-1) here because n can be 1
getrandbits
=
self
.
getrandbits
getrandbits
=
self
.
getrandbits
# Only call self.getrandbits if the original random() builtin method
# Only call self.getrandbits if the original random() builtin method
# has not been overridden or if a new getrandbits() was supplied.
# has not been overridden or if a new getrandbits() was supplied.
if
type
(
self
.
random
)
is
BuiltinMethod
or
type
(
getrandbits
)
is
Method
:
if
type
(
self
.
random
)
is
BuiltinMethod
or
type
(
getrandbits
)
is
Method
:
k
=
n
.
bit_length
()
# don't use (n-1) here because n can be 1
r
=
getrandbits
(
k
)
# 0 <= r < 2**k
r
=
getrandbits
(
k
)
# 0 <= r < 2**k
while
r
>=
n
:
while
r
>=
n
:
r
=
getrandbits
(
k
)
r
=
getrandbits
(
k
)
return
r
return
r
# There's an overriden random() method but no new getrandbits() method,
# There's an overriden random() method but no new getrandbits() method,
# so we can only use random() from here.
# so we can only use random() from here.
if
k
>
bpf
:
random
=
self
.
random
if
n
>=
maxsize
:
_warn
(
"Underlying random() generator does not supply
\n
"
_warn
(
"Underlying random() generator does not supply
\n
"
"enough bits to choose from a population range this large.
\n
"
"enough bits to choose from a population range this large.
\n
"
"To remove the range limitation, add a getrandbits() method."
)
"To remove the range limitation, add a getrandbits() method."
)
return
int
(
self
.
random
()
*
n
)
return
int
(
random
()
*
n
)
r
andom
=
self
.
random
r
em
=
maxsize
%
n
N
=
1
<<
k
limit
=
(
maxsize
-
rem
)
/
maxsize
# int(limit * maxsize) % n == 0
r
=
int
(
N
*
random
())
# 0 <= r < 2**k
r
=
random
()
while
r
>=
n
:
while
r
>=
limit
:
r
=
int
(
N
*
random
()
)
r
=
random
(
)
return
r
return
int
(
r
*
maxsize
)
%
n
## -------------------- sequence methods -------------------
## -------------------- sequence methods -------------------
...
...
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