diff options
author | Raymond Hettinger <python@rcn.com> | 2003-01-04 09:26:32 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-01-04 09:26:32 (GMT) |
commit | 73ced7ee995180c0bd8d96ff7d7fb614a744ad7d (patch) | |
tree | 6f947e5610c1d232c7b2b1267b8ca919331ca01e /Lib/random.py | |
parent | 3a57d9de076d255f5cf46816c07e79a8c9b76ec4 (diff) | |
download | cpython-73ced7ee995180c0bd8d96ff7d7fb614a744ad7d.zip cpython-73ced7ee995180c0bd8d96ff7d7fb614a744ad7d.tar.gz cpython-73ced7ee995180c0bd8d96ff7d7fb614a744ad7d.tar.bz2 |
Correct long standing bugs in the methods for random distributions.
The range of u=random() is [0,1), so log(u) and 1/x can fail.
Fix by setting u=1-random() or by reselecting for a usable value.
Will backport.
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/Lib/random.py b/Lib/random.py index 03dadf2..4ddac4c 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -282,7 +282,7 @@ class Random(CoreGenerator): random = self.random while True: u1 = random() - u2 = random() + u2 = 1.0 - random() z = NV_MAGICCONST*(u1-0.5)/u2 zz = z*z/4.0 if zz <= -_log(u2): @@ -422,7 +422,9 @@ class Random(CoreGenerator): while True: u1 = random() - u2 = random() + if not 1e-7 < u1 < .9999999: + continue + u2 = 1.0 - random() v = _log(u1/(1.0-u1))/ainv x = alpha*_exp(v) z = u1*u1*u2 @@ -554,7 +556,7 @@ class Random(CoreGenerator): """Pareto distribution. alpha is the shape parameter.""" # Jain, pg. 495 - u = self.random() + u = 1.0 - self.random() return 1.0 / pow(u, 1.0/alpha) ## -------------------- Weibull -------------------- @@ -567,7 +569,7 @@ class Random(CoreGenerator): """ # Jain, pg. 499; bug fix courtesy Bill Arms - u = self.random() + u = 1.0 - self.random() return alpha * pow(-_log(u), 1.0/beta) ## -------------------- Wichmann-Hill ------------------- |