Skip to content

random.vonmisesvariate raises ZeroDivisionError for large finite kappa #158742

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Random.vonmisesvariate docstring: "mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa is the concentration parameter, which must be greater than or equal to zero." Random class docstring explicitly permits subclassing to "use a different basic generator of your own devising" by overriding random() and the state methods.

Expected: Return a finite circular sample without raising an exception.

Actual: ZeroDivisionError: division by zero

import math
import random

mu, kappa = 0.0, 1e20
values = (1.0 - 2**-53, 0.5, 0.5, 0.5)
input_ = dict(mu=mu, kappa=kappa, random_values=values)

class R(random.Random):
    def random(self):
        return next(self.values, 0.5)

valid = (math.isfinite(mu) and 0 <= mu <= math.tau
         and math.isfinite(kappa) and kappa >= 0
         and all(0 <= x < 1 for x in values))
if not valid:
    print("REFUTATION REJECTED:", "input violates documented domain", input_)
else:
    r = R()
    r.values = iter(values)
    expected = f"finite circular sample in [0, {2 * math.pi}] without an exception"
    try:
        actual = r.vonmisesvariate(mu, kappa)
        broken = not (math.isfinite(actual) and 0 <= actual <= 2 * math.pi)
    except Exception as e:
        actual = (type(e).__name__, str(e))
        broken = True
    if broken:
        print("REFUTATION CONFIRMED:", input_, "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED:", "call returned a valid sample", actual)

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library random:

REFUTATION CONFIRMED: {'mu': 0.0, 'kappa': 1e+20, 'random_values': (0.9999999999999999, 0.5, 0.5, 0.5)} actual: ('ZeroDivisionError', 'division by zero') expected: finite circular sample in [0, 6.283185307179586] without an exception

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/random-20261003-034345-c2

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions