summaryrefslogtreecommitdiff
authortino <ttk448@gmail.com>2012-12-06 14:05:11 (GMT)
committer Markus Mohrhard <markus.mohrhard@googlemail.com>2012-12-08 01:27:06 (GMT)
commit8450a99c744e9005f19173e4df35d65640bcf5c4 (patch) (side-by-side diff)
treed6f5d194c256f3d3790701a9f423b7e8eb99b3a8
parente9b21193ed395061b155955e5098f69af92b4fc0 (diff)
downloadcore-8450a99c744e9005f19173e4df35d65640bcf5c4.zip
core-8450a99c744e9005f19173e4df35d65640bcf5c4.tar.gz
fdo#33365 added wrapper for boost random, use that in RAND()
Change-Id: Iafc524d12c76423f74dc16b42595e52fbc5a1e54
Diffstat (more/less context) (ignore whitespace changes)
-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 348300f..10918e4 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 4e449c5..e18d241 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 0000000..a9f6c81
--- a/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 43a21eb..d382152 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 0000000..a7fff67
--- a/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: */