diff Lib/random.py @ 7412:297bea233e03 legacy-trunk

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.
author Guido van Rossum <guido@python.org>
date Thu, 19 Feb 1998 21:17:42 +0000
parents bca948194dea
children 6370f1593c72
line wrap: on
line diff
--- a/Lib/random.py	Thu Feb 19 21:15:44 1998 +0000
+++ b/Lib/random.py	Thu Feb 19 21:17:42 1998 +0000
@@ -182,12 +182,13 @@
 	# 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 @@
 		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