summaryrefslogtreecommitdiff
path: root/formula
diff options
context:
space:
mode:
authorTor Lillqvist <tml@collabora.com>2015-01-20 16:24:14 +0200
committerMichael Meeks <michael.meeks@collabora.com>2015-01-21 07:21:19 +0000
commitdad7161cc30b335a108c7850f453877a161969ae (patch)
treeb0b093ffe95b869a94fe3de283e33baff7254c4c /formula
parentcd5f69376bf27275d92073943451b8b72a708d04 (diff)
fdo88632: Make the Calc random functions non-random when requested
We don't want such a mode to affect other uses of randomness, though. Thus use a separate random number generator object for these two functions, and use a fixed seed for it if the SC_RAND_REPEATABLE environment variable is set. As RAND() is implemented in sc, and RANDBETWEEN() is implemented in scaddins, it was a bit hard to figure out where to add the new functions needed, without having to over-engineer things with UNO. (This functionality is totally Calc-specific, but neither sc nor scaddins has any public (non-UNO) API.) Caolan suggested the formula module, which seems like a good enough place to me. Change-Id: Ic1c9ca165278d53036598b03b13b4ffbdc98a75e Reviewed-on: https://gerrit.libreoffice.org/14053 Reviewed-by: Michael Meeks <michael.meeks@collabora.com> Tested-by: Michael Meeks <michael.meeks@collabora.com>
Diffstat (limited to 'formula')
-rw-r--r--formula/Library_for.mk1
-rw-r--r--formula/README6
-rw-r--r--formula/source/core/api/random.cxx56
3 files changed, 61 insertions, 2 deletions
diff --git a/formula/Library_for.mk b/formula/Library_for.mk
index d2a6671d3b20..85cf5e8d2036 100644
--- a/formula/Library_for.mk
+++ b/formula/Library_for.mk
@@ -41,6 +41,7 @@ $(eval $(call gb_Library_set_componentfile,for,formula/util/for))
$(eval $(call gb_Library_add_exception_objects,for,\
formula/source/core/api/FormulaCompiler \
formula/source/core/api/FormulaOpCodeMapperObj \
+ 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: */