diff options
-rw-r--r-- | formula/Library_for.mk | 1 | ||||
-rw-r--r-- | formula/README | 6 | ||||
-rw-r--r-- | formula/source/core/api/random.cxx | 56 | ||||
-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 |
8 files changed, 99 insertions, 7 deletions
diff --git a/formula/Library_for.mk b/formula/Library_for.mk index ad7da5af3445..cbdff466535f 100644 --- a/formula/Library_for.mk +++ b/formula/Library_for.mk @@ -42,6 +42,7 @@ $(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/README b/formula/README index da0d528c1cad..ab2c28a08c42 100644 --- a/formula/README +++ b/formula/README @@ -1,3 +1,5 @@ -Contains parts of the formula parser used outside Calc code. +Contains parts of the formula parser used outside Calc code that has +been pulled out from Calc's formula parser code. -Has been pulled out from Calc's formula parser code. +Also contains some functions that are needed by code in both sc and +scaddins. Located here just for convenience. So sue me. diff --git a/formula/source/core/api/random.cxx b/formula/source/core/api/random.cxx new file mode 100644 index 000000000000..727262fdffd5 --- /dev/null +++ b/formula/source/core/api/random.cxx @@ -0,0 +1,56 @@ +/* -*- 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/formula/random.hxx b/include/formula/random.hxx new file mode 100644 index 000000000000..d5d14f54ced9 --- /dev/null +++ b/include/formula/random.hxx @@ -0,0 +1,31 @@ +/* -*- 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 0bb4ad66f305..e76bc1b0ebef 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 <comphelper/random.hxx> +#include <formula/random.hxx> #include <formula/vectortoken.hxx> #include "scmatrix.hxx" @@ -703,7 +703,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 = comphelper::rng::uniform_int_distribution(0, SAL_MAX_INT32); + cl_int seed = formula::rng::nRandom(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), (void*)&seed); diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx index 2c58fcc965e4..16d00523c961 100644 --- a/sc/source/core/tool/interpr1.cxx +++ b/sc/source/core/tool/interpr1.cxx @@ -22,6 +22,7 @@ #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> @@ -1662,7 +1663,7 @@ void ScInterpreter::ScPi() void ScInterpreter::ScRandom() { - PushDouble(::comphelper::rng::uniform_real_distribution()); + PushDouble(formula::rng::fRandom(0, 1)); } void ScInterpreter::ScTrue() diff --git a/scaddins/Library_analysis.mk b/scaddins/Library_analysis.mk index cafce60604a7..4946c5f74107 100644 --- a/scaddins/Library_analysis.mk +++ b/scaddins/Library_analysis.mk @@ -33,6 +33,7 @@ $(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 20edfc679949..f7b7fe8f62e0 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> @@ -703,7 +703,7 @@ double SAL_CALL AnalysisAddIn::getRandbetween( double fMin, double fMax ) throw( if( fMin > fMax ) throw lang::IllegalArgumentException(); - double fRet = floor(comphelper::rng::uniform_real_distribution(fMin, nextafter(fMax+1, -DBL_MAX))); + double fRet = floor(formula::rng::fRandom(fMin, nextafter(fMax+1, -DBL_MAX))); RETURN_FINITE( fRet ); } |