Skip to content

Add an optional seed to the samplers' random generators - #51

Open
f-dy wants to merge 3 commits into
danini:masterfrom
f-dy:feature/random-seed
Open

f-dy wants to merge 3 commits into
danini:masterfrom
f-dy:feature/random-seed

Conversation

@f-dy

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

Copy link
Copy Markdown

Each sampler (uniform, PROSAC, P-NAPSAC, NAPSAC, importance) seeds its random generator, UniformRandomGenerator, from std::random_device, so runs can't be repeated. This PR adds UniformRandomGenerator<size_t>::setGlobalSeed(seed): the generators created afterwards are seeded with seed, seed + 1, seed + 2, …, so a run started after the same seed draws the same samples. clearGlobalSeed() goes back to std::random_device. Without a call to either, nothing changes.

danini/magsac#48 uses it to add a seed argument to MAGSAC's Python estimators.

Changes

  • uniform_random_generator.h: the seed of the next generator, used and incremented by the constructor.
  • Each generator gets its own seed: P-NAPSAC creates three (its own and those of its two nested PROSAC samplers), which would otherwise produce the same stream.
  • The seed is per thread (thread_local): a seed set in one thread doesn't affect generators created in others, and threads don't race on the counter.
  • Samplers that draw from std::rand (the AR-Sampler) aren't affected: callers seed them with std::srand.

Testing

  • New C++ test tests/cpp/test_uniform_random_generator.cpp, built with the new CMake option BUILD_TESTS (off by default) and run with ctest. It checks that two generators created after one seed differ, that the same seed gives them again, that P-NAPSAC's own generator doesn't repeat the stream its first PROSAC sampler gets, and that unseeded generators differ. It passes, and fails on the previous version of this PR, where every generator got the same seed.
  • With Add an optional random seed to the Python estimators magsac#48 on top: the same seed gives the same model and inlier mask for all five MAGSAC estimators and all samplers (macOS, Apple clang 17).

The other open PRs of this repository (#50, #52, #53, #56) touch other files and merge cleanly with this one, in any order.

Updates after review: the comment of setGlobalSeed now says that the seed applies only to the generators created in the calling thread.

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
…seeds

With a seed, every generator got the same one, so P-NAPSAC's three
generators (its own and those of its two nested PROSAC samplers) produced
the same stream. They now get seed, seed + 1, seed + 2, ... The seed is
per thread (thread_local), so threads don't race on the counter.

Test: tests/cpp/test_uniform_random_generator.cpp (CMake option BUILD_TESTS,
run with ctest). It fails on the previous version.
@f-dy f-dy changed the title feat: add a process-wide random seed to UniformRandomGenerator Add an optional seed to the samplers' random generators Nov 14, 2026
@f-dy
f-dy marked this pull request as ready for review November 14, 2026 08:57
f-dy added a commit to f-dy/magsac that referenced this pull request Nov 14, 2026
…lable (73b2f5a)

It merges upstream master and danini/graph-cut-ransac#51 (random seed, used by danini#48), danini#52 (line solver's null sample, needed by danini#45, danini#52 and danini#55), danini#53, danini#56, danini#59 and danini#61. The commit is only on the fork, hence .gitmodules' fork URL.
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