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
81fd6ca9
Commit
81fd6ca9
authored
Mar 09, 1994
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added gauss() (same as normal but twice as fast) and betavariate();
print more statistics in test_generator()
parent
f4784f5a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
6 deletions
+47
-6
Lib/random.py
Lib/random.py
+47
-6
No files found.
Lib/random.py
View file @
81fd6ca9
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
# lognormal
# lognormal
# negative exponential
# negative exponential
# gamma
# gamma
# beta
#
#
# distributions on the circle (angles 0 to 2pi)
# distributions on the circle (angles 0 to 2pi)
# ---------------------------------------------
# ---------------------------------------------
...
@@ -15,7 +16,7 @@
...
@@ -15,7 +16,7 @@
# Translated from anonymously contributed C/C++ source.
# Translated from anonymously contributed C/C++ source.
from
whrandom
import
random
,
uniform
,
randint
,
choice
# Also for export!
from
whrandom
import
random
,
uniform
,
randint
,
choice
# Also for export!
from
math
import
log
,
exp
,
pi
,
e
,
sqrt
,
acos
,
cos
from
math
import
log
,
exp
,
pi
,
e
,
sqrt
,
acos
,
cos
,
sin
# Housekeeping function to verify that magic constants have been
# Housekeeping function to verify that magic constants have been
# computed correctly
# computed correctly
...
@@ -172,6 +173,37 @@ def stdgamma(alpha, ainv, bbb, ccc):
...
@@ -172,6 +173,37 @@ def stdgamma(alpha, ainv, bbb, ccc):
break
break
return
x
return
x
# -------------------- Gauss (faster alternative) --------------------
# When x and y are two variables from [0, 1), uniformly distributed, then
#
# cos(2*pi*x)*log(1-y)
# sin(2*pi*x)*log(1-y)
#
# are two *independent* variables with normal distribution (mu = 0, sigma = 1).
# (Lambert Meertens)
gauss_next
=
None
def
gauss
(
mu
,
sigma
):
global
gauss_next
if
gauss_next
!=
None
:
z
=
gauss_next
gauss_next
=
None
else
:
x2pi
=
random
()
*
TWOPI
log1_y
=
log
(
1.0
-
random
())
z
=
cos
(
x2pi
)
*
log1_y
gauss_next
=
sin
(
x2pi
)
*
log1_y
return
mu
+
z
*
sigma
# -------------------- beta --------------------
def
betavariate
(
alpha
,
beta
):
y
=
expovariate
(
alpha
)
z
=
expovariate
(
1.0
/
beta
)
return
z
/
(
y
+
z
)
# -------------------- test program --------------------
# -------------------- test program --------------------
def
test
():
def
test
():
...
@@ -179,7 +211,7 @@ def test():
...
@@ -179,7 +211,7 @@ def test():
print
'LOG4 ='
,
LOG4
print
'LOG4 ='
,
LOG4
print
'NV_MAGICCONST ='
,
NV_MAGICCONST
print
'NV_MAGICCONST ='
,
NV_MAGICCONST
print
'SG_MAGICCONST ='
,
SG_MAGICCONST
print
'SG_MAGICCONST ='
,
SG_MAGICCONST
N
=
1
00
N
=
2
00
test_generator
(
N
,
'random()'
)
test_generator
(
N
,
'random()'
)
test_generator
(
N
,
'normalvariate(0.0, 1.0)'
)
test_generator
(
N
,
'normalvariate(0.0, 1.0)'
)
test_generator
(
N
,
'lognormvariate(0.0, 1.0)'
)
test_generator
(
N
,
'lognormvariate(0.0, 1.0)'
)
...
@@ -192,21 +224,30 @@ def test():
...
@@ -192,21 +224,30 @@ def test():
test_generator
(
N
,
'gammavariate(2.0, 1.0)'
)
test_generator
(
N
,
'gammavariate(2.0, 1.0)'
)
test_generator
(
N
,
'gammavariate(20.0, 1.0)'
)
test_generator
(
N
,
'gammavariate(20.0, 1.0)'
)
test_generator
(
N
,
'gammavariate(200.0, 1.0)'
)
test_generator
(
N
,
'gammavariate(200.0, 1.0)'
)
test_generator
(
N
,
'gauss(0.0, 1.0)'
)
test_generator
(
N
,
'betavariate(3.0, 3.0)'
)
def
test_generator
(
n
,
funccall
):
def
test_generator
(
n
,
funccall
):
import
sys
import
time
print
'%d calls to %s:'
%
(
n
,
funccall
),
print
n
,
'times'
,
funccall
sys
.
stdout
.
flush
()
code
=
compile
(
funccall
,
funccall
,
'eval'
)
code
=
compile
(
funccall
,
funccall
,
'eval'
)
sum
=
0.0
sum
=
0.0
sqsum
=
0.0
sqsum
=
0.0
smallest
=
1e10
largest
=
1e-10
t0
=
time
.
time
()
for
i
in
range
(
n
):
for
i
in
range
(
n
):
x
=
eval
(
code
)
x
=
eval
(
code
)
sum
=
sum
+
x
sum
=
sum
+
x
sqsum
=
sqsum
+
x
*
x
sqsum
=
sqsum
+
x
*
x
smallest
=
min
(
x
,
smallest
)
largest
=
max
(
x
,
largest
)
t1
=
time
.
time
()
print
round
(
t1
-
t0
,
3
),
'sec,'
,
avg
=
sum
/
n
avg
=
sum
/
n
stddev
=
sqrt
(
sqsum
/
n
-
avg
*
avg
)
stddev
=
sqrt
(
sqsum
/
n
-
avg
*
avg
)
print
'avg %g, stddev %g'
%
(
avg
,
stddev
)
print
'avg %g, stddev %g, min %g, max %g'
%
\
(
avg
,
stddev
,
smallest
,
largest
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
test
()
test
()
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