Skip to content

feat: add a pluggable RNG seam for native embedders#423

Draft
antoneri wants to merge 2 commits intomasterfrom
feat/411-pluggable-rng
Draft

feat: add a pluggable RNG seam for native embedders#423
antoneri wants to merge 2 commits intomasterfrom
feat/411-pluggable-rng

Conversation

@antoneri
Copy link
Copy Markdown
Member

@antoneri antoneri commented Apr 8, 2026

Closes #411

Summary

This PR adds a native RNG injection seam for embedders while keeping Infomap's default behavior unchanged.

  • make Random type-erased and copyable so it can wrap a seedable standard-engine-compatible RNG
  • add a native-only setRandomEngine(...) API on InfomapConfig
  • preserve the injected RNG type across setConfig(...), reseed(...), and subnetwork/non-main Infomap creation
  • add lifecycle tests covering custom RNG injection, reseeding, config updates, and subnetwork inheritance

Why

igraph currently carries a downstream patch to replace Infomap's internal RNG with its own RNG. The existing implementation hardcodes std::mt19937 into core runtime code, which makes that integration awkward and requires patching algorithm-adjacent code.

This change introduces a narrow upstream-supported seam so downstream consumers can adapt their own RNG implementation without patching the optimizer logic.

Embedder example

#include "Infomap.h"

#include <cstdint>
#include <limits>

struct HostRng {
  using result_type = std::uint32_t;

  std::uint32_t state = 0;

  void seed(unsigned int seedValue)
  {
    state = seedValue;
  }

  static constexpr result_type min() { return 0; }
  static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }

  result_type operator()()
  {
    state = state * 1664525u + 1013904223u;
    return state;
  }
};

int main()
{
  infomap::InfomapWrapper im("--silent --num-trials 1");

  im.setRandomEngine(HostRng{});
  im.reseed(123);

  im.addLink(1, 2);
  im.addLink(2, 3);
  im.addLink(3, 1);

  im.run();
}

Impact

  • default CLI and library behavior still uses std::mt19937
  • the existing seed-based workflow remains intact for current users
  • native embedders now have a documented hook for supplying their own RNG engine
  • Python and JS bindings are unchanged

Validation

  • make build-native
  • make test-native CTEST_ARGS='-R "infomap_cpp_lifecycle_tests"'
  • make test-native CTEST_ARGS='-R "infomap_cpp_config_tests|infomap_cpp_lifecycle_tests"'

@antoneri antoneri changed the title [codex] Add a pluggable RNG seam for native embedders feat: add a pluggable RNG seam for native embedders Apr 8, 2026
@antoneri
Copy link
Copy Markdown
Member Author

antoneri commented Apr 8, 2026

@szhorvat please take a look!

@szhorvat
Copy link
Copy Markdown

szhorvat commented Apr 9, 2026

@antoneri I will! We're running oral exams here so it's a bit busy, I might only get to it on the weekend. Thanks so much for making these improvements!

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.

Make the RNG pluggable through a standard engine-compatible interface

2 participants