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
bbc50eaf
Kaydet (Commit)
bbc50eaf
authored
Mar 23, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 1681432: Add triangular distribution the random module.
üst
9a0d3462
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
1 deletion
+31
-1
random.rst
Doc/library/random.rst
+4
-0
random.py
Lib/random.py
+23
-1
test_random.py
Lib/test/test_random.py
+2
-0
NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/random.rst
Dosyayı görüntüle @
bbc50eaf
...
...
@@ -190,6 +190,10 @@ be found in any statistics text.
Return a random floating point number *N* such that ``a <= N < b``.
.. function:: triangular(low, high, mode)
Return a random floating point number *N* such that ``low <= N < high``
and with the specified *mode* between those bounds.
.. function:: betavariate(alpha, beta)
...
...
Lib/random.py
Dosyayı görüntüle @
bbc50eaf
...
...
@@ -13,6 +13,7 @@
distributions on the real line:
------------------------------
uniform
triangular
normal (Gaussian)
lognormal
negative exponential
...
...
@@ -47,7 +48,7 @@ from binascii import hexlify as _hexlify
__all__
=
[
"Random"
,
"seed"
,
"random"
,
"uniform"
,
"randint"
,
"choice"
,
"sample"
,
"randrange"
,
"shuffle"
,
"normalvariate"
,
"lognormvariate"
,
"expovariate"
,
"vonmisesvariate"
,
"gammavariate"
,
"expovariate"
,
"vonmisesvariate"
,
"gammavariate"
,
"triangular"
,
"gauss"
,
"betavariate"
,
"paretovariate"
,
"weibullvariate"
,
"getstate"
,
"setstate"
,
"jumpahead"
,
"WichmannHill"
,
"getrandbits"
,
"SystemRandom"
]
...
...
@@ -350,6 +351,25 @@ class Random(_random.Random):
"""Get a random number in the range [a, b)."""
return
a
+
(
b
-
a
)
*
self
.
random
()
## -------------------- triangular --------------------
def
triangular
(
self
,
low
,
high
,
mode
):
"""Triangular distribution.
Continuous distribution bounded by given lower and upper limits,
and having a given mode value in-between.
http://en.wikipedia.org/wiki/Triangular_distribution
"""
u
=
self
.
random
()
c
=
(
mode
-
low
)
/
(
high
-
low
)
if
u
>
c
:
u
=
1
-
u
c
=
1
-
c
low
,
high
=
high
,
low
return
low
+
(
high
-
low
)
*
(
u
*
c
)
**
0.5
## -------------------- normal distribution --------------------
def
normalvariate
(
self
,
mu
,
sigma
):
...
...
@@ -839,6 +859,7 @@ def _test(N=2000):
_test_generator
(
N
,
gammavariate
,
(
200.0
,
1.0
))
_test_generator
(
N
,
gauss
,
(
0.0
,
1.0
))
_test_generator
(
N
,
betavariate
,
(
3.0
,
3.0
))
_test_generator
(
N
,
triangular
,
(
0.0
,
1.0
,
1.0
/
3.0
))
# Create one instance, seeded from current time, and export its methods
# as module-level functions. The functions share state across all uses
...
...
@@ -850,6 +871,7 @@ _inst = Random()
seed
=
_inst
.
seed
random
=
_inst
.
random
uniform
=
_inst
.
uniform
triangular
=
_inst
.
triangular
randint
=
_inst
.
randint
choice
=
_inst
.
choice
randrange
=
_inst
.
randrange
...
...
Lib/test/test_random.py
Dosyayı görüntüle @
bbc50eaf
...
...
@@ -488,6 +488,7 @@ class TestDistributions(unittest.TestCase):
g
.
random
=
x
[:]
.
pop
;
g
.
gammavariate
(
1.0
,
1.0
)
g
.
random
=
x
[:]
.
pop
;
g
.
gammavariate
(
200.0
,
1.0
)
g
.
random
=
x
[:]
.
pop
;
g
.
betavariate
(
3.0
,
3.0
)
g
.
random
=
x
[:]
.
pop
;
g
.
triangular
(
0.0
,
1.0
,
1.0
/
3.0
)
def
test_avg_std
(
self
):
# Use integration to test distribution average and standard deviation.
...
...
@@ -497,6 +498,7 @@ class TestDistributions(unittest.TestCase):
x
=
[
i
/
float
(
N
)
for
i
in
xrange
(
1
,
N
)]
for
variate
,
args
,
mu
,
sigmasqrd
in
[
(
g
.
uniform
,
(
1.0
,
10.0
),
(
10.0
+
1.0
)
/
2
,
(
10.0
-
1.0
)
**
2
/
12
),
(
g
.
triangular
,
(
0.0
,
1.0
,
1.0
/
3.0
),
4.0
/
9.0
,
7.0
/
9.0
/
18.0
),
(
g
.
expovariate
,
(
1.5
,),
1
/
1.5
,
1
/
1.5
**
2
),
(
g
.
paretovariate
,
(
5.0
,),
5.0
/
(
5.0
-
1
),
5.0
/
((
5.0
-
1
)
**
2
*
(
5.0
-
2
))),
...
...
Misc/NEWS
Dosyayı görüntüle @
bbc50eaf
...
...
@@ -66,6 +66,8 @@ Library
- Issue #2432: give DictReader the dialect and line_num attributes
advertised in the docs.
- Issue #1681432: Add triangular distribution to the random module
- Issue #2136: urllib2's auth handler now allows single-quoted realms in the
WWW-Authenticate header.
...
...
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