Kaydet (Commit) 8450a99c authored tarafından tino's avatar tino Kaydeden (comit) Markus Mohrhard

fdo#33365 added wrapper for boost random, use that in RAND()

Change-Id: Iafc524d12c76423f74dc16b42595e52fbc5a1e54
üst e9b21193
...@@ -213,6 +213,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\ ...@@ -213,6 +213,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
sc/source/core/tool/progress \ sc/source/core/tool/progress \
sc/source/core/tool/queryentry \ sc/source/core/tool/queryentry \
sc/source/core/tool/queryparam \ sc/source/core/tool/queryparam \
sc/source/core/tool/random \
sc/source/core/tool/rangelst \ sc/source/core/tool/rangelst \
sc/source/core/tool/rangenam \ sc/source/core/tool/rangenam \
sc/source/core/tool/rangeseq \ sc/source/core/tool/rangeseq \
......
...@@ -75,6 +75,7 @@ ...@@ -75,6 +75,7 @@
#include "sc.hrc" #include "sc.hrc"
#include "scmod.hxx" #include "scmod.hxx"
#include "appoptio.hxx" #include "appoptio.hxx"
#include "random.hxx"
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
...@@ -557,6 +558,7 @@ void ScGlobal::Init() ...@@ -557,6 +558,7 @@ void ScGlobal::Init()
// names from the compiler. // names from the compiler.
ScParameterClassification::Init(); ScParameterClassification::Init();
srand( (unsigned) time( NULL ) ); // Random Seed Init fuer Interpreter srand( (unsigned) time( NULL ) ); // Random Seed Init fuer Interpreter
sc::rng::seed( time( NULL ) ); // seed for libc rand() replacement
InitAddIns(); InitAddIns();
......
/* -*- 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: */
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#include "globstr.hrc" #include "globstr.hrc"
#include "attrib.hxx" #include "attrib.hxx"
#include "jumpmatrix.hxx" #include "jumpmatrix.hxx"
#include "random.hxx"
#include <comphelper/processfactory.hxx> #include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx> #include <comphelper/string.hxx>
...@@ -1711,7 +1712,7 @@ void ScInterpreter::ScPi() ...@@ -1711,7 +1712,7 @@ void ScInterpreter::ScPi()
void ScInterpreter::ScRandom() void ScInterpreter::ScRandom()
{ {
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRandom" ); RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRandom" );
PushDouble((double)rand() / ((double)RAND_MAX+1.0)); PushDouble(sc::rng::uniform());
} }
......
/* -*- 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: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment