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

Skip to content

Commit be5f919

Browse files
committed
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
1 parent 497cee4 commit be5f919

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
@@ -475,6 +475,20 @@ def test_avg_std(self):
475475
self.assertAlmostEqual(s1/N, mu, places=2)
476476
self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2)
477477

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

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ Core and Builtins
218218
Library
219219
-------
220220

221+
- Issue #17149: Fix random.vonmisesvariate to always return results in
222+
[0, 2*math.pi].
223+
221224
- Issue #1470548: XMLGenerator now works with binary output streams.
222225

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

0 commit comments

Comments
 (0)