summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-04-06 14:12:13 (GMT)
committerGuido van Rossum <guido@python.org>1998-04-06 14:12:13 (GMT)
commit5810297052003f28788f6790ac799fe8e5373494 (patch)
treebbfc51a298ec8d9e5f17982e1515c8e292e763ee /Lib/random.py
parent56c04b837654c2dc51788a6d6a90a9f58e3dd57a (diff)
downloadcpython-5810297052003f28788f6790ac799fe8e5373494.zip
cpython-5810297052003f28788f6790ac799fe8e5373494.tar.gz
cpython-5810297052003f28788f6790ac799fe8e5373494.tar.bz2
Correction to vonmisesvariate() by Magnus Kessler: it should take and
return something between 0 and 2*pi. Also added a reference to the literature.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 49921cb..221bef6 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -79,10 +79,17 @@ TWOPI = 2.0*pi
verify('TWOPI', 6.28318530718)
def vonmisesvariate(mu, kappa):
- # mu: mean angle (in radians between 0 and 180 degrees)
+ # mu: mean angle (in radians between 0 and 2*pi)
# kappa: concentration parameter kappa (>= 0)
-
# if kappa = 0 generate uniform random angle
+
+ # Based upon an algorithm published in: Fisher, N.I.,
+ # "Statistical Analysis of Circular Data", Cambridge
+ # University Press, 1993.
+
+ # Thanks to Magnus Kessler for a correction to the
+ # implementation of step 4.
+
if kappa <= 1e-6:
return TWOPI * random()
@@ -104,11 +111,11 @@ def vonmisesvariate(mu, kappa):
u3 = random()
if u3 > 0.5:
- theta = mu + 0.5*acos(f)
+ theta = (mu % TWOPI) + acos(f)
else:
- theta = mu - 0.5*acos(f)
+ theta = (mu % TWOPI) - acos(f)
- return theta % pi
+ return theta
# -------------------- gamma distribution --------------------