summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-02-19 21:17:42 (GMT)
committerGuido van Rossum <guido@python.org>1998-02-19 21:17:42 (GMT)
commit72c2e1b56e35c7fc4a80e90b14541494426e3cd0 (patch)
tree8f5c655d228e06a202b13a46569e29a5c47b07e1 /Lib/random.py
parent9824509d3ee84f00ba658e249bad81e00e3b98bf (diff)
downloadcpython-72c2e1b56e35c7fc4a80e90b14541494426e3cd0.zip
cpython-72c2e1b56e35c7fc4a80e90b14541494426e3cd0.tar.gz
cpython-72c2e1b56e35c7fc4a80e90b14541494426e3cd0.tar.bz2
Fixed a bug in the gauss() function. The bug was reported by Mike
Miller, who complained that its kurtosis was bad, and then fixed by Lambert Meertens (author of the original algorithm) who discovered that the mathematical analysis leading to his solution was wrong, and provided a corrected version. Mike then tested the fix and reported that the kurtosis was now good.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/Lib/random.py b/Lib/random.py
index ebab1f8..49921cb 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -182,12 +182,13 @@ def gauss(mu, sigma):
# 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)
+ # cos(2*pi*x)*sqrt(-2*log(1-y))
+ # sin(2*pi*x)*sqrt(-2*log(1-y))
#
# are two *independent* variables with normal distribution
# (mu = 0, sigma = 1).
# (Lambert Meertens)
+ # (corrected version; bug discovered by Mike Miller, fixed by LM)
global gauss_next
@@ -196,9 +197,9 @@ def gauss(mu, sigma):
gauss_next = None
else:
x2pi = random() * TWOPI
- log1_y = log(1.0 - random())
- z = cos(x2pi) * log1_y
- gauss_next = sin(x2pi) * log1_y
+ g2rad = sqrt(-2.0 * log(1.0 - random()))
+ z = cos(x2pi) * g2rad
+ gauss_next = sin(x2pi) * g2rad
return mu + z*sigma