diff options
| author | Raymond Hettinger <python@rcn.com> | 2003-01-04 09:30:32 +0000 |
|---|---|---|
| committer | Raymond Hettinger <python@rcn.com> | 2003-01-04 09:30:32 +0000 |
| commit | 63b5156e6e4a01737c333265772c677035fce657 (patch) | |
| tree | 3dd41ae1903a87797c4d8c1cf66439c974fea76c /Lib/random.py | |
| parent | 65b5e1fdbebe4ca35b665bc22bc51e9a806b4669 (diff) | |
| download | cpython-git-63b5156e6e4a01737c333265772c677035fce657.tar.gz | |
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.
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 948b6935cf..85aa723644 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -399,7 +399,7 @@ class Random: random = self.random while 1: u1 = random() - u2 = random() + u2 = 1.0 - random() z = NV_MAGICCONST*(u1-0.5)/u2 zz = z*z/4.0 if zz <= -_log(u2): @@ -535,7 +535,9 @@ class Random: while 1: 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 @@ -667,7 +669,7 @@ class Random: """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 -------------------- @@ -680,7 +682,7 @@ class Random: """ # Jain, pg. 499; bug fix courtesy Bill Arms - u = self.random() + u = 1.0 - self.random() return alpha * pow(-_log(u), 1.0/beta) ## -------------------- test program -------------------- |
