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
b760efb0
Commit
b760efb0
authored
May 14, 2002
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closes patch 529408 deprecating random.stdgamma().
parent
bdc8289e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
11 deletions
+35
-11
Lib/random.py
Lib/random.py
+35
-11
No files found.
Lib/random.py
View file @
b760efb0
...
...
@@ -445,14 +445,15 @@ class Random:
## -------------------- gamma distribution --------------------
def
gammavariate
(
self
,
alpha
,
beta
):
# beta times standard gamma
return
beta
*
self
.
stdgamma
(
alpha
)
def
stdgamma
(
self
,
alpha
,
*
args
):
# *args for Py2.2 compatiblity
# alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
# Warning: a few older sources define the gamma distribution in terms
# of alpha > -1.0
if
alpha
<=
0.0
or
beta
<=
0.0
:
raise
ValueError
,
'gammavariate: alpha and beta must be > 0.0'
random
=
self
.
random
if
alpha
<=
0.0
:
raise
ValueError
,
'stdgamma: alpha must be > 0.0'
if
alpha
>
1.0
:
# Uses R.C.H. Cheng, "The generation of Gamma
...
...
@@ -471,14 +472,14 @@ class Random:
z
=
u1
*
u1
*
u2
r
=
bbb
+
ccc
*
v
-
x
if
r
+
SG_MAGICCONST
-
4.5
*
z
>=
0.0
or
r
>=
_log
(
z
):
return
x
return
x
*
beta
elif
alpha
==
1.0
:
# expovariate(1)
u
=
random
()
while
u
<=
1e-7
:
u
=
random
()
return
-
_log
(
u
)
return
-
_log
(
u
)
*
beta
else
:
# alpha is between 0 and 1 (exclusive)
...
...
@@ -497,7 +498,27 @@ class Random:
if
not
(((
p
<=
1.0
)
and
(
u1
>
_exp
(
-
x
)))
or
((
p
>
1
)
and
(
u1
>
pow
(
x
,
alpha
-
1.0
)))):
break
return
x
return
x
*
beta
def
stdgamma
(
self
,
alpha
,
ainv
,
bbb
,
ccc
):
# This method was (and shall remain) undocumented.
# This method is deprecated
# for the following reasons:
# 1. Returns same as .gammavariate(alpha, 1.0)
# 2. Requires caller to provide 3 extra arguments
# that are functions of alpha anyway
# 3. Can't be used for alpha < 0.5
# ainv = sqrt(2 * alpha - 1)
# bbb = alpha - log(4)
# ccc = alpha + ainv
import
warnings
warnings
.
warn
(
"The stdgamma function is deprecated; "
"use gammavariate() instead"
,
DeprecationWarning
)
return
self
.
gammavariate
(
alpha
,
1.0
)
## -------------------- Gauss (faster alternative) --------------------
...
...
@@ -596,7 +617,7 @@ def _test_generator(n, funccall):
print
'avg %g, stddev %g, min %g, max %g'
%
\
(
avg
,
stddev
,
smallest
,
largest
)
def
_test
(
N
=
200
):
def
_test
(
N
=
200
00
):
print
'TWOPI ='
,
TWOPI
print
'LOG4 ='
,
LOG4
print
'NV_MAGICCONST ='
,
NV_MAGICCONST
...
...
@@ -607,6 +628,9 @@ def _test(N=200):
_test_generator
(
N
,
'cunifvariate(0.0, 1.0)'
)
_test_generator
(
N
,
'expovariate(1.0)'
)
_test_generator
(
N
,
'vonmisesvariate(0.0, 1.0)'
)
_test_generator
(
N
,
'gammavariate(0.01, 1.0)'
)
_test_generator
(
N
,
'gammavariate(0.1, 1.0)'
)
_test_generator
(
N
,
'gammavariate(0.1, 2.0)'
)
_test_generator
(
N
,
'gammavariate(0.5, 1.0)'
)
_test_generator
(
N
,
'gammavariate(0.9, 1.0)'
)
_test_generator
(
N
,
'gammavariate(1.0, 1.0)'
)
...
...
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