Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6427358

Browse files
committed
Issue #17149: merge fix from 3.2.
2 parents bf95c14 + be5f919 commit 6427358

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

Lib/random.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,9 @@ def vonmisesvariate(self, mu, kappa):
449449

450450
u3 = random()
451451
if u3 > 0.5:
452-
theta = (mu % TWOPI) + _acos(f)
452+
theta = (mu + _acos(f)) % TWOPI
453453
else:
454-
theta = (mu % TWOPI) - _acos(f)
454+
theta = (mu - _acos(f)) % TWOPI
455455

456456
return theta
457457

Lib/test/test_random.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,20 @@ def test_avg_std(self):
479479
self.assertAlmostEqual(s1/N, mu, places=2)
480480
self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2)
481481

482+
def test_von_mises_range(self):
483+
# Issue 17149: von mises variates were not consistently in the
484+
# range [0, 2*PI].
485+
g = random.Random()
486+
N = 100
487+
for mu in 0.0, 0.1, 3.1, 6.2:
488+
for kappa in 0.0, 2.3, 500.0:
489+
for _ in range(N):
490+
sample = g.vonmisesvariate(mu, kappa)
491+
self.assertTrue(
492+
0 <= sample <= random.TWOPI,
493+
msg=("vonmisesvariate({}, {}) produced a result {} out"
494+
" of range [0, 2*pi]").format(mu, kappa, sample))
495+
482496
class TestModule(unittest.TestCase):
483497
def testMagicConstants(self):
484498
self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ Core and Builtins
172172
Library
173173
-------
174174

175+
- Issue #17149: Fix random.vonmisesvariate to always return results in
176+
[0, 2*math.pi].
177+
175178
- Issue #1470548: XMLGenerator now works with binary output streams.
176179

177180
- Issue #6975: os.path.realpath() now correctly resolves multiple nested

0 commit comments

Comments
 (0)