[Libreoffice-commits] .: Branch 'libreoffice-4-0' - sc/Library_sc.mk sc/source

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Mon Dec 10 07:30:41 PST 2012


 sc/Library_sc.mk                 |    1 
 sc/source/core/data/global.cxx   |    2 +
 sc/source/core/inc/random.hxx    |   29 +++++++++++++++++++++
 sc/source/core/tool/interpr1.cxx |    3 +-
 sc/source/core/tool/random.cxx   |   53 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 87 insertions(+), 1 deletion(-)

New commits:
commit d421e1d25393416708f7c3fba625bd057deb9e08
Author: Tino Kluge <ttk448 at gmail.com>
Date:   Thu Dec 6 14:05:11 2012 +0000

    fdo#33365 added wrapper for boost random, use that in RAND()
    
    Change-Id: Iafc524d12c76423f74dc16b42595e52fbc5a1e54

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
--- /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
--- /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 at 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: */


More information about the Libreoffice-commits mailing list