Refactor(rng): migrate core distributions from TRandom3 to std::mt19937_64#472
Open
HimanKing wants to merge 4 commits into
Open
Refactor(rng): migrate core distributions from TRandom3 to std::mt19937_64#472HimanKing wants to merge 4 commits into
HimanKing wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Partially migrates BioDynaMo's random number generation subsystem away from
ROOT's
TRandom3by replacing the backing engine withstd::mt19937_64andre-implementing the majority of distributions using C++17 standard library
equivalents in
src/core/util/random.handsrc/core/util/random.cc.Motivation
ROOT is a large external dependency that significantly increases build
complexity, installation time, and reduces portability — particularly for
researchers without access to high-specification hardware or system
administration expertise. This refactoring targets the random number generation
subsystem as a self-contained, well-tested module suitable for incremental
dependency reduction.This is the first step toward breaking BioDynaMo's
dependency on ROOT, proving the migration path is viable one subsystem at a time.
Files Modified
src/core/util/random.hsrc/core/util/random.cctest/unit/core/util/random_test.cctest/unit/core/simulation_test.ccChanges
RNG Engine
TRandom3as the primary sampling engine withstd::mt19937_64(
mt_engine_).GetEngine()public accessor returning a reference tomt_engine_,allowing
DistributionRngsubclasses to draw samples without requiringfriend access or exposing private members.
SetSeed()seeds bothmt_engine_and the retainedTRandom3instance(
generator_) with the same value, keeping unmigrated distributionsreproducible under the same seed call.
GetSeed()now returnslast_seed_— auint64_tmember set directly bySetSeed(). This removes the previous delegation togenerator_->GetSeed()(TRandom3) and ensures the returned value is authoritative and independent of
the ROOT engine, including if
GetEngine()is used to interact withmt_engine_directly.Distributions Migrated to stdlib
Uniform()TRandom3::Uniformstd::uniform_real_distributionGaus()TRandom3::Gausstd::normal_distributionExp()TRandom3::Expstd::exponential_distributionPoissonD()TRandom3::PoissonDstd::poisson_distribution<long>Poisson()TRandom3::Poissonstd::poisson_distribution<int>Binomial()TRandom3::Binomialstd::binomial_distributionInteger()TRandom3::Integerstd::uniform_int_distributionCircle()TRandom3::Circlestd::uniform_real_distributionSphere()TRandom3::Spherestd::normal_distribution)Distributions Not Yet Migrated (future work)
Landau()— no standard library equivalent; still delegates togenerator_->Landau(). Requires a custom rejection-sampling implementation.BreitWigner()— no standard library equivalent; still delegates togenerator_->BreitWigner().UserDefinedDistRng1D/2D/3D— user-defined distributions backed byROOT's
TF1/TF2/TF3, sampled viaGetRandom(). These use ROOT's globalgRandomengine internally and are unaffected bySetSeed()on aRandominstance. A full replacement requires a rejection-sampling framework and is
deferred.
Architectural Note — Stale Virtual Interface
The
DistributionRng<T>::SampleImpl(TRandom* rng)virtual interface retainsits ROOT-typed signature. Implementations that have been migrated to stdlib
silently ignore the
rngparameter (marked/*rng*/in source). Thisinterface will be updated to remove the
TRandom*parameter once alldistributions are ported.
Testing
All unit tests pass under the refactored implementation alongside the full
suite of system-wide unit tests: