Skip to content

Feat: seed support for reproducible sampling #81

@wrdxwrdxwrdx

Description

@wrdxwrdxwrdx

Summary

DefaultUnuranSampler has no way to seed the RNG. All instances share one process-global
numpy.random.default_rng() (see urng.py), making results non-reproducible and tests
order-dependent. initialization.py:241 has a # TODO seed support placeholder.


Problems

  1. Non-reproducibility — same distribution, different output every run.
  2. Shared global state — sampling from one instance advances the state seen by all others.
  3. No per-instance isolation — impossible to have two independent samplers in the same process.

Proposed API

Add seed: int | np.random.Generator | None = None to UnuranMethodConfig. Each sampler
constructs its own Generator via np.random.default_rng(seed) and registers a per-instance
UNUR_URNG (the C API already exposes unur_urng_new). The handle must be freed in
cleanup() alongside the generator.


Thread safety

numpy.random.Generator is not thread-safe. The implementation must ensure:

  • Each sampler instance owns an independent Generator; never share one across threads.
  • Same seed passed to two samplers in different threads must produce identical sequences in
    isolation — achieved by constructing a fresh Generator per instance, not resetting a shared one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions