diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-02-10 17:29:54 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-02-10 17:29:54 (GMT) |
commit | 06e5e730dd802d4560880e1a2ed69d2bbca6be2f (patch) | |
tree | 195236484f4ba6b394a5753d9b2005edb40162f5 /Lib/random.py | |
parent | b8cbba5877df478966092ce069b6cedea8487c53 (diff) | |
parent | ac99576a8eda27d7554c5293df22d29ec9a2043d (diff) | |
download | cpython-06e5e730dd802d4560880e1a2ed69d2bbca6be2f.zip cpython-06e5e730dd802d4560880e1a2ed69d2bbca6be2f.tar.gz cpython-06e5e730dd802d4560880e1a2ed69d2bbca6be2f.tar.bz2 |
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/Lib/random.py b/Lib/random.py index 0e67ddb..b2e9ae1 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -432,22 +432,20 @@ class Random(_random.Random): if kappa <= 1e-6: return TWOPI * random() - a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa) - b = (a - _sqrt(2.0 * a))/(2.0 * kappa) - r = (1.0 + b * b)/(2.0 * b) + s = 0.5 / kappa + r = s + _sqrt(1.0 + s * s) while 1: u1 = random() - z = _cos(_pi * u1) - f = (1.0 + r * z)/(r + z) - c = kappa * (r - f) + d = z / (r + z) u2 = random() - - if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c): + if u2 < 1.0 - d * d or u2 <= (1.0 - d) * _exp(d): break + q = 1.0 / r + f = (q + z) / (1.0 + q * z) u3 = random() if u3 > 0.5: theta = (mu + _acos(f)) % TWOPI |