Skip to content

feat: add a process-wide random seed to UniformRandomGenerator#51

Draft
f-dy wants to merge 1 commit into
danini:masterfrom
f-dy:feature/random-seed
Draft

feat: add a process-wide random seed to UniformRandomGenerator#51
f-dy wants to merge 1 commit into
danini:masterfrom
f-dy:feature/random-seed

Conversation

@f-dy

@f-dy f-dy commented May 31, 2026

Copy link
Copy Markdown

feat: add a process-wide random seed to UniformRandomGenerator

What

Adds UniformRandomGenerator::setGlobalSeed(uint64_t) and clearGlobalSeed()
(src/pygcransac/include/uniform_random_generator.h). When a seed is set, every
subsequently constructed generator is seeded deterministically
(std::mt19937(seed)) instead of from std::random_device; clearing restores the
default. Default behavior (no seed) is unchanged.

This mirrors OpenCV's cv::setRNGSeed — a process-wide reproducibility switch.

Why

There is currently no way to make RANSAC sampling reproducible, which makes
debugging non-deterministic failures hard. A fixed seed gives repeatable runs.

Coverage

  • All URG-based samplers (UniformSampler, ProsacSampler,
    ProgressiveNapsacSampler, ImportanceSampler) build their generator via the
    default UniformRandomGenerator() constructor, so they honor the global seed
    with no per-sampler change.
  • Samplers/solvers that use std::rand() / Eigen::…::Random()
    (AdaptiveReorderingSampler, PnP bundle-adjustment, DLS/SIFT solvers) are not
    driven by this generator; the caller makes those reproducible with
    std::srand(seed) (Eigen::Random uses std::rand by default).
  • Caveat: composite samplers that hold nested generators (Progressive-NAPSAC)
    will seed each nested generator with the same value → correlated sub-sequences.
    Fine for reproducible debugging; deriving distinct sub-seeds is a possible
    later refinement.

Companion PR

This is the enabling change. The seed is exposed to users through the seed
parameter added to the pymagsac Python API in the companion magsac PR,
danini/magsac#48, which calls
setGlobalSeed(seed) + std::srand(seed) before running. That PR depends on
this one
(and bumps the submodule to this commit).

Adds UniformRandomGenerator::setGlobalSeed(seed) / clearGlobalSeed(), mirroring
cv::setRNGSeed. When a seed is set, every subsequently constructed generator is
seeded deterministically (std::mt19937(seed)) instead of from
std::random_device; clearing restores nondeterministic behavior. Default
behavior is unchanged.

This makes RANSAC sampling reproducible for debugging. All URG-based samplers
(Uniform, PROSAC, Progressive-NAPSAC, Importance) construct their generator via
the default constructor, so they all honor the seed with no per-sampler change.
Samplers/solvers that use std::rand()/Eigen::Random (AdaptiveReordering, PnP
bundle adjustment, DLS/SIFT) are made reproducible by the caller via
std::srand(seed).

Enables the seed parameter exposed in the pymagsac Python API (companion
magsac PR).
f-dy added a commit to f-dy/magsac that referenced this pull request May 31, 2026
Adds a 'seed' argument (default -1 = nondeterministic) to all 14 find*
estimators. When seed >= 0 the run is reproducible: a shared helper
(applyMagsacSeed) sets UniformRandomGenerator::setGlobalSeed(seed) for all
URG-based samplers and std::srand(seed) for the std::rand()/Eigen::Random
solver paths. Bumps the graph-cut-ransac submodule to include
setGlobalSeed/clearGlobalSeed.

Combines danini#48 (seed for the pre-existing estimators) and
danini/graph-cut-ransac#51 (the seed mechanism), extended to the new
estimators added in danini#45 (incl. findPlane3D).
@f-dy f-dy marked this pull request as ready for review May 31, 2026 22:21
@f-dy f-dy marked this pull request as draft May 31, 2026 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant