summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTino Kluge <ttk448@gmail.com>2012-12-06 14:05:11 +0000
committerKohei Yoshida <kohei.yoshida@gmail.com>2012-12-10 09:14:55 -0500
commitd421e1d25393416708f7c3fba625bd057deb9e08 (patch)
tree092359dc665be3c11862e9f2185535d22c65781f
parent9cce41e668de5b50e05d762b88f3bbafb3bb641a (diff)
fdo#33365 added wrapper for boost random, use that in RAND()
Change-Id: Iafc524d12c76423f74dc16b42595e52fbc5a1e54
-rw-r--r--sc/Library_sc.mk1
-rw-r--r--sc/source/core/data/global.cxx2
-rw-r--r--sc/source/core/inc/random.hxx29
-rw-r--r--sc/source/core/tool/interpr1.cxx3
-rw-r--r--sc/source/core/tool/random.cxx53
5 files changed, 87 insertions, 1 deletions
diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk
index 348300fcd80c..10918e493f9d 100644
--- a/sc/Library_sc.mk
+++ b/sc/Library_sc.mk
@@ -213,6 +213,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
sc/source/core/tool/progress \
sc/source/core/tool/queryentry \
sc/source/core/tool/queryparam \
+ sc/source/core/tool/random \
sc/source/core/tool/rangelst \
sc/source/core/tool/rangenam \
sc/source/core/tool/rangeseq \
diff --git a/sc/source/core/data/global.cxx b/sc/source/core/data/global.cxx
index 4e449c5a5dd2..e18d241ba54b 100644
--- a/sc/source/core/data/global.cxx
+++ b/sc/source/core/data/global.cxx
@@ -75,6 +75,7 @@
#include "sc.hrc"
#include "scmod.hxx"
#include "appoptio.hxx"
+#include "random.hxx"
// -----------------------------------------------------------------------
@@ -557,6 +558,7 @@ void ScGlobal::Init()
// names from the compiler.
ScParameterClassification::Init();
srand( (unsigned) time( NULL ) ); // Random Seed Init fuer Interpreter
+ sc::rng::seed( time( NULL ) ); // seed for libc rand() replacement
InitAddIns();
diff --git a/sc/source/core/inc/random.hxx b/sc/source/core/inc/random.hxx
new file mode 100644
index 000000000000..a9f6c813bddd
--- /dev/null
+++ b/sc/source/core/inc/random.hxx
@@ -0,0 +1,29 @@
+/* -*- 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 SC_RANDOM_HXX
+#define SC_RANDOM_HXX
+
+namespace sc
+{
+
+namespace rng
+{
+
+void seed(int i); // set initial seed (equivalent of libc srand())
+
+double uniform(); // uniform distribution in [0,1)
+
+} // namespace
+
+} // namespace
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx
index 43a21ebe28be..d382152d4a9f 100644
--- a/sc/source/core/tool/interpr1.cxx
+++ b/sc/source/core/tool/interpr1.cxx
@@ -44,6 +44,7 @@
#include "globstr.hrc"
#include "attrib.hxx"
#include "jumpmatrix.hxx"
+#include "random.hxx"
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
@@ -1711,7 +1712,7 @@ void ScInterpreter::ScPi()
void ScInterpreter::ScRandom()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRandom" );
- PushDouble((double)rand() / ((double)RAND_MAX+1.0));
+ PushDouble(sc::rng::uniform());
}
diff --git a/sc/source/core/tool/random.cxx b/sc/source/core/tool/random.cxx
new file mode 100644
index 000000000000..a7fff67f5e70
--- /dev/null
+++ b/sc/source/core/tool/random.cxx
@@ -0,0 +1,53 @@
+/* -*- 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/.
+ *
+ * Contributor(s):
+ * Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
+ *
+ */
+
+#include <boost/random.hpp>
+
+// this is nothing but a simple wrapper around
+// the boost random generators
+
+namespace sc
+{
+namespace rng
+{
+
+// underlying random number generator
+// boost::mt19937 implements the Mersenne twister algorithm which
+// is fast and has good statistical properties, it produces integers
+// in the range of [0, 2^32-1] internally
+// memory requirement: 625*sizeof(uint32_t)
+// http://en.wikipedia.org/wiki/Mersenne_twister
+#define BOOST_RNG_ALGO boost::mt19937
+BOOST_RNG_ALGO global_rng;
+
+// initialises the state of the global random number generator
+// should only be called once at the start of the main programme
+// (note, a few boost::variate_generator<> (like normal) have their
+// own state which would need a reset as well to guarantee identical
+// sequence of numbers, e.g. via myrand.distribution().reset())
+void seed(int i)
+{
+ global_rng.seed(i);
+}
+
+// uniform [0,1) or [a,b) distribution
+double uniform()
+{
+ static boost::uniform_01<BOOST_RNG_ALGO&> myrand(global_rng);
+ return myrand();
+}
+
+} // namespace
+} // namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */