diff options
author | Caolán McNamara <caolanm@redhat.com> | 2015-07-09 14:48:31 +0100 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2015-07-10 08:58:20 +0100 |
commit | 5b2f8231945fedc46425e00f1234dcac90628c1d (patch) | |
tree | 7cdee4b82bd29d59292d0a4bd4df4e816317192c | |
parent | 315afb12853624bdaac553a8528390c3a61c8351 (diff) |
add a SAL_RAND_REPEATABLE for repeatable random nums
merge the formula and comphelper ones together
Change-Id: I2e7e2cdb176afc6982e384fa1e007da5b914e6f0
-rw-r--r-- | comphelper/source/misc/random.cxx | 7 | ||||
-rw-r--r-- | formula/Library_for.mk | 1 | ||||
-rw-r--r-- | formula/source/core/api/random.cxx | 56 | ||||
-rw-r--r-- | include/comphelper/random.hxx | 3 | ||||
-rw-r--r-- | include/formula/random.hxx | 31 | ||||
-rw-r--r-- | sc/source/core/opencl/formulagroupcl.cxx | 4 | ||||
-rw-r--r-- | sc/source/core/tool/interpr1.cxx | 3 | ||||
-rw-r--r-- | scaddins/Library_analysis.mk | 1 | ||||
-rw-r--r-- | scaddins/source/analysis/analysis.cxx | 4 |
9 files changed, 15 insertions, 95 deletions
diff --git a/comphelper/source/misc/random.cxx b/comphelper/source/misc/random.cxx index 0c6e83edd20a..a70a73e3584b 100644 --- a/comphelper/source/misc/random.cxx +++ b/comphelper/source/misc/random.cxx @@ -39,6 +39,13 @@ struct RandomNumberGenerator STD_RNG_ALGO global_rng; RandomNumberGenerator() { + bool bRepeatable = (getenv("SAL_RAND_REPEATABLE") != 0); + if (bRepeatable) + { + global_rng.seed(42); + return; + } + try { std::random_device rd; diff --git a/formula/Library_for.mk b/formula/Library_for.mk index cbdff466535f..ad7da5af3445 100644 --- a/formula/Library_for.mk +++ b/formula/Library_for.mk @@ -42,7 +42,6 @@ $(eval $(call gb_Library_add_exception_objects,for,\ formula/source/core/api/FormulaCompiler \ formula/source/core/api/FormulaOpCodeMapperObj \ formula/source/core/api/grammar \ - formula/source/core/api/random \ formula/source/core/api/services \ formula/source/core/api/token \ formula/source/core/api/vectortoken \ diff --git a/formula/source/core/api/random.cxx b/formula/source/core/api/random.cxx deleted file mode 100644 index 727262fdffd5..000000000000 --- a/formula/source/core/api/random.cxx +++ /dev/null @@ -1,56 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#include <time.h> - -#include <random> - -#include <formula/random.hxx> -#include <rtl/instance.hxx> - -namespace { - -struct CalcFormulaRandomGenerator -{ - std::mt19937 aRng; - CalcFormulaRandomGenerator() - { - // initialises the state of this RNG. - // should only be called once. - bool bRepeatable = (getenv("SC_RAND_REPEATABLE") != 0); - aRng.seed(bRepeatable ? 42 : time(NULL)); - } -}; - -class theCalcFormulaRandomGenerator : public rtl::Static<CalcFormulaRandomGenerator, theCalcFormulaRandomGenerator> {}; - -} - -namespace formula -{ - -namespace rng -{ - -double fRandom(double a, double b) -{ - std::uniform_real_distribution<double> dist(a, b); - return dist(theCalcFormulaRandomGenerator::get().aRng); -} - -sal_Int32 nRandom(sal_Int32 a, sal_Int32 b) -{ - std::uniform_int_distribution<sal_Int32> dist(a, b); - return dist(theCalcFormulaRandomGenerator::get().aRng); -} - -} // rng -} // formula - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/comphelper/random.hxx b/include/comphelper/random.hxx index 080b5d246891..218d61683a1c 100644 --- a/include/comphelper/random.hxx +++ b/include/comphelper/random.hxx @@ -18,6 +18,9 @@ namespace comphelper namespace rng { +// These functions obey the SAL_RAND_REPEATABLE environment +// variable: If it is set, use a fixed seed. + // note that uniform_int_distribution is inclusive of b, i.e. [a,b] while // uniform_real_distribution is exclusive of b, i.e. [a,b), std::nextafter may be your friend there diff --git a/include/formula/random.hxx b/include/formula/random.hxx deleted file mode 100644 index d5d14f54ced9..000000000000 --- a/include/formula/random.hxx +++ /dev/null @@ -1,31 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#ifndef INCLUDED_FORMULA_RANDOM_HXX -#define INCLUDED_FORMULA_RANDOM_HXX - -#include <formula/formuladllapi.h> - -namespace formula -{ - -namespace rng -{ - -// These two functions obey the SC_RAND_REPEATABLE environment -// variable: If it is set, use a fixed seed. -double FORMULA_DLLPUBLIC fRandom(double a, double b); -sal_Int32 FORMULA_DLLPUBLIC nRandom(sal_Int32 a, sal_Int32 b); - -} // rng -} // formula - -#endif // INCLUDED_FORMULA_RANDOM_HXX - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/opencl/formulagroupcl.cxx b/sc/source/core/opencl/formulagroupcl.cxx index 98c95e7cad99..ed03cce25350 100644 --- a/sc/source/core/opencl/formulagroupcl.cxx +++ b/sc/source/core/opencl/formulagroupcl.cxx @@ -16,7 +16,7 @@ #include "tokenarray.hxx" #include "compiler.hxx" #include "interpre.hxx" -#include <formula/random.hxx> +#include <comphelper/random.hxx> #include <formula/vectortoken.hxx> #include "scmatrix.hxx" @@ -738,7 +738,7 @@ threefry2x32 (threefry2x32_ctr_t in, threefry2x32_key_t k)\n\ /// Create buffer and pass the buffer to a given kernel virtual size_t Marshal( cl_kernel k, int argno, int, cl_program ) SAL_OVERRIDE { - cl_int seed = formula::rng::nRandom(0, SAL_MAX_INT32); + cl_int seed = comphelper::rng::uniform_int_distribution(0, SAL_MAX_INT32); // Pass the scalar result back to the rest of the formula kernel SAL_INFO("sc.opencl", "Kernel " << k << " arg " << argno << ": cl_int: " << seed); cl_int err = clSetKernelArg(k, argno, sizeof(cl_int), static_cast<void*>(&seed)); diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx index e1dfd2afa93d..78922695f522 100644 --- a/sc/source/core/tool/interpr1.cxx +++ b/sc/source/core/tool/interpr1.cxx @@ -22,7 +22,6 @@ #include "scitems.hxx" #include <editeng/langitem.hxx> #include <editeng/justifyitem.hxx> -#include <formula/random.hxx> #include <osl/thread.h> #include <svx/algitem.hxx> #include <unotools/textsearch.hxx> @@ -1653,7 +1652,7 @@ void ScInterpreter::ScPi() void ScInterpreter::ScRandom() { - PushDouble(formula::rng::fRandom(0, 1)); + PushDouble(::comphelper::rng::uniform_real_distribution()); } void ScInterpreter::ScTrue() diff --git a/scaddins/Library_analysis.mk b/scaddins/Library_analysis.mk index 4946c5f74107..cafce60604a7 100644 --- a/scaddins/Library_analysis.mk +++ b/scaddins/Library_analysis.mk @@ -33,7 +33,6 @@ $(eval $(call gb_Library_use_libraries,analysis,\ comphelper \ cppu \ cppuhelper \ - for \ sal \ tl \ i18nlangtag \ diff --git a/scaddins/source/analysis/analysis.cxx b/scaddins/source/analysis/analysis.cxx index d6d30474e244..08ac8620e7d7 100644 --- a/scaddins/source/analysis/analysis.cxx +++ b/scaddins/source/analysis/analysis.cxx @@ -22,8 +22,8 @@ #include "bessel.hxx" #include <cppuhelper/factory.hxx> #include <comphelper/processfactory.hxx> +#include <comphelper/random.hxx> #include <cppuhelper/supportsservice.hxx> -#include <formula/random.hxx> #include <osl/diagnose.h> #include <rtl/ustrbuf.hxx> #include <rtl/math.hxx> @@ -693,7 +693,7 @@ double SAL_CALL AnalysisAddIn::getRandbetween( double fMin, double fMax ) throw( if( fMin > fMax ) throw lang::IllegalArgumentException(); - double fRet = floor(formula::rng::fRandom(fMin, nextafter(fMax+1, -DBL_MAX))); + double fRet = floor(comphelper::rng::uniform_real_distribution(fMin, nextafter(fMax+1, -DBL_MAX))); RETURN_FINITE( fRet ); } |