summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-02-10 17:29:20 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-02-10 17:29:20 (GMT)
commitac99576a8eda27d7554c5293df22d29ec9a2043d (patch)
treeb868719081a180b33bc0e39e10e4fd52e25ad545 /Lib/random.py
parent801d955f04d46994ac5bc7270fea86a7703c5192 (diff)
parent6c22b1d7609413f711cb1bcf258ecc13ef15af07 (diff)
downloadcpython-ac99576a8eda27d7554c5293df22d29ec9a2043d.zip
cpython-ac99576a8eda27d7554c5293df22d29ec9a2043d.tar.gz
cpython-ac99576a8eda27d7554c5293df22d29ec9a2043d.tar.bz2
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py14
1 files changed, 6 insertions, 8 deletions
diff --git a/Lib/random.py b/Lib/random.py
index c275071..7d8d4f3 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -431,22 +431,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