From 8fd18bd4616ab06049aa0c760b9248c04373863f Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Mon, 4 Mar 2024 20:46:42 +0000 Subject: tdf#160056 comphelper::rng takes a mutex for every random number MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic4b50a9ea9e9eda62ca0877337462354b3fe3675 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164403 Tested-by: Caolán McNamara Reviewed-by: Caolán McNamara --- sc/source/core/inc/interpre.hxx | 6 +++++- sc/source/core/tool/interpr1.cxx | 30 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) (limited to 'sc/source') diff --git a/sc/source/core/inc/interpre.hxx b/sc/source/core/inc/interpre.hxx index ebff66afafe0..b92833b9535d 100644 --- a/sc/source/core/inc/interpre.hxx +++ b/sc/source/core/inc/interpre.hxx @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -231,6 +232,7 @@ private: ScCalcConfig maCalcConfig; formula::FormulaTokenIterator aCode; + std::optional oRNG; ScAddress aPos; ScTokenArray* pArr; ScInterpreterContext& mrContext; @@ -552,6 +554,8 @@ private: // Returns true if last jump was executed and result matrix pushed. bool JumpMatrix( short nStackLevel ); + std::mt19937& GetRNG(); + double Compare( ScQueryOp eOp ); /** @param pOptions NULL means case sensitivity document option is to be used! @@ -576,7 +580,7 @@ private: void ScPi(); void ScRandom(); void ScRandbetween(); - void ScRandomImpl( const std::function& RandomFunc, + void ScRandomImpl( const std::function& RandomFunc, double fFirst, double fLast ); void ScTrue(); void ScFalse(); diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx index f6c5409bdbc3..0a9ee65ff37c 100644 --- a/sc/source/core/tool/interpr1.cxx +++ b/sc/source/core/tool/interpr1.cxx @@ -1737,7 +1737,7 @@ void ScInterpreter::ScPi() PushDouble(M_PI); } -void ScInterpreter::ScRandomImpl( const std::function& RandomFunc, +void ScInterpreter::ScRandomImpl( const std::function& RandomFunc, double fFirst, double fLast ) { if (bMatrixFormula) @@ -1764,7 +1764,7 @@ void ScInterpreter::ScRandomImpl( const std::functionPutDouble( RandomFunc( fFirst, fLast), + pResMat->PutDouble( RandomFunc( GetRNG(), fFirst, fLast), static_cast(i), static_cast(j)); } } @@ -1794,15 +1794,28 @@ void ScInterpreter::ScRandomImpl( const std::function::max())); + oRNG = std::mt19937(nSeed); + } + return *oRNG; +} + void ScInterpreter::ScRandom() { - auto RandomFunc = []( double, double ) + auto RandomFunc = [](std::mt19937& rRNG, double, double) { - return comphelper::rng::uniform_real_distribution(); + std::uniform_real_distribution dist(0.0, 1.0); + return dist(rRNG); }; ScRandomImpl( RandomFunc, 0.0, 0.0); } @@ -1822,9 +1835,10 @@ void ScInterpreter::ScRandbetween() return; } fMax = std::nextafter( fMax+1, -DBL_MAX); - auto RandomFunc = []( double fFirst, double fLast ) + auto RandomFunc = [](std::mt19937& rRNG, double fFirst, double fLast) { - return floor( comphelper::rng::uniform_real_distribution( fFirst, fLast)); + std::uniform_real_distribution dist(fFirst, fLast); + return floor(dist(rRNG)); }; ScRandomImpl( RandomFunc, fMin, fMax); } -- cgit